Skip to content

Commit

Permalink
Merge pull request #9 from replicatedhq/import-zone
Browse files Browse the repository at this point in the history
Adding page rules to import
  • Loading branch information
diamonwiggins authored Jul 7, 2021
2 parents 93ec3d9 + 4895345 commit aaae1f0
Show file tree
Hide file tree
Showing 7 changed files with 130 additions and 11 deletions.
17 changes: 17 additions & 0 deletions docs/docs/getting-started/api-support.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Cloudflare API Support Matrix

Cloudflare has a lot of objects and supports configuring them through their API.
The Kubeflare controller is early and doesn't (yet) have support for all Cloudflare objects.
Our intention is to support them all, including enterprise-only functionality.

The table below is the current state of support for the Cloudflare API.
If an object is not included on the table, it's not supported yet.
Feel free to open an [issue](https://github.com/replicatedhq/kubeflare/issues/new) if there's a specific API you need or would like to help with.

| Cloudflare API | Status | Kubeflare Version |
|----------------|--------|-------------------|
| Zone Settings | Completed | 0.1.0 |
| DNS Records | Completed | 0.1.0 |
| PageRules | In Prgress | 0.1.0 |
| Access Applications | In Progress | 0.1.0 |
| Web Application Firewall | In Progress | 0.1.0 |
1 change: 1 addition & 0 deletions docs/docs/getting-started/importing.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,5 +44,6 @@ The full list of optional types to exclude is in the table below:
| Flag | Record Type(s)
|------|---------------
| `--dns-records` | All DNS Records
| `--page-rules` | All PageRules


2 changes: 1 addition & 1 deletion docs/docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,4 @@ This is where you want to get started.

Need more help?

- Submit an issue on [GitHub](https://github.com/replicatedhq/kubeflare).
- Submit an issue on [GitHub](https://github.com/replicatedhq/kubeflare).
1 change: 1 addition & 0 deletions docs/mkdocs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ markdown_extensions:
nav:
- Getting Started:
- 'Introduction': 'index.md'
- 'Cloudflare API Support': 'getting-started/api-support.md'
- 'Importing': 'getting-started/importing.md'
- Installing:
- 'Install with kubectl': 'install/kubectl.md'
Expand Down
21 changes: 21 additions & 0 deletions pkg/cli/kubeflarecli/import.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"github.com/pkg/errors"
kubeflarescheme "github.com/replicatedhq/kubeflare/pkg/client/kubeflareclientset/scheme"
"github.com/replicatedhq/kubeflare/pkg/cloudflare/dns"
"github.com/replicatedhq/kubeflare/pkg/cloudflare/pagerules"
"github.com/spf13/cobra"
"github.com/spf13/viper"
serializer "k8s.io/apimachinery/pkg/runtime/serializer/json"
Expand Down Expand Up @@ -70,6 +71,25 @@ func ImportCmd() *cobra.Command {
}
}
}

if v.GetBool("page-rules") {
pageRules, err := pagerules.FetchPageRulesForZone(v.GetString("api-token"), v.GetString("zone"), zoneID)
if err != nil {
return errors.Wrap(err, "fetch page rules")
}

for _, pageRule := range pageRules {
buf := bytes.NewBuffer(nil)
err := s.Encode(pageRule, buf)
if err != nil {
return errors.Wrap(err, "encode")
}
outputFile := filepath.Join(v.GetString("output-dir"), fmt.Sprintf("%s.yaml", pageRule.Name))
if err := ioutil.WriteFile(outputFile, buf.Bytes(), 0644); err != nil {
return errors.Wrap(err, "write file")
}
}
}
return nil
},
}
Expand All @@ -83,6 +103,7 @@ func ImportCmd() *cobra.Command {
cmd.Flags().String("output-dir", filepath.Join(".", "imported"), "output dir to write files to")

cmd.Flags().Bool("dns-records", true, "when set, import existing dns records from the zone")
cmd.Flags().Bool("page-rules", true, "when set, import existing page rules from the zone")

return cmd
}
20 changes: 10 additions & 10 deletions pkg/cloudflare/dns/import.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,30 +13,30 @@ func FetchDNSRecordsForZone(token string, zone string, zoneID string) ([]*v1alph
return nil, errors.Wrap(err, "create clouflare client")
}

records, err := cf.DNSRecords(zoneID, cloudflare.DNSRecord{})
resources, err := cf.DNSRecords(zoneID, cloudflare.DNSRecord{})
if err != nil {
return nil, errors.Wrap(err, "fetch records")
return nil, errors.Wrap(err, "fetch resources")
}

dnsRecords := []*v1alpha1.DNSRecord{}
for _, record := range records {
for _, resource := range resources {
dnsRecord := v1alpha1.DNSRecord{
TypeMeta: metav1.TypeMeta{
APIVersion: "crds.kubeflare.io/v1alpha1",
Kind: "DNSRecord",
},
ObjectMeta: metav1.ObjectMeta{
Name: record.Name,
Name: resource.Name,
},
Spec: v1alpha1.DNSRecordSpec{
Zone: zone,
Record: &v1alpha1.Record{
Type: record.Type,
Name: record.Name,
Content: record.Content,
TTL: &record.TTL,
Priority: &record.Priority,
Proxied: &record.Proxied,
Type: resource.Type,
Name: resource.Name,
Content: resource.Content,
TTL: &resource.TTL,
Priority: &resource.Priority,
Proxied: &resource.Proxied,
},
},
}
Expand Down
79 changes: 79 additions & 0 deletions pkg/cloudflare/pagerules/import.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
package pagerules

import (
"github.com/cloudflare/cloudflare-go"
"github.com/pkg/errors"
"github.com/replicatedhq/kubeflare/pkg/apis/crds/v1alpha1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

func FetchPageRulesForZone(token string, zone string, zoneID string) ([]*v1alpha1.PageRule, error) {
cf, err := cloudflare.NewWithAPIToken(token)
if err != nil {
return nil, errors.Wrap(err, "create clouflare client")
}

resources, err := cf.ListPageRules(zoneID)
if err != nil {
return nil, errors.Wrap(err, "fetch resources")
}

pageRules := []*v1alpha1.PageRule{}
for _, resource := range resources {
spec := v1alpha1.PageRuleSpec{
Zone: zone,
}

for _, action := range resource.Actions {
if action.ID == "forwarding_url" {
spec.Rule = &v1alpha1.Rule{
ForwardingURL: &v1alpha1.ForwardingURLPageRule{
RedirectURL: action.Value.(map[string]interface{})["url"].(string),
StatusCode: int(action.Value.(map[string]interface{})["status_code"].(float64)),
},
}
} else if action.ID == "always_use_https" {
spec.Rule = &v1alpha1.Rule{
AlwaysUseHTTPS: &v1alpha1.AlwaysUseHTTPSPageRule{},
}
}
}

if spec.Rule == nil {
// we don't support this type of rule yet
// prob should do something like add it
continue
}

if resource.Status == "enabled" {
spec.Rule.Enabled = true
}

spec.Rule.Priority = &resource.Priority

for _, target := range resource.Targets {
if target.Target == "url" {
// this is a poor and partial implementation todo
spec.Rule.RequestURL = target.Constraint.Value
}
}

pageRule := v1alpha1.PageRule{
TypeMeta: metav1.TypeMeta{
APIVersion: "crds.kubeflare.io/v1alpha1",
Kind: "PageRule",
},
ObjectMeta: metav1.ObjectMeta{
Name: resource.ID, // TODO, this could be better
},
Spec: spec,
Status: v1alpha1.PageRuleStatus{
ID: resource.ID,
},
}

pageRules = append(pageRules, &pageRule)
}

return pageRules, nil
}

0 comments on commit aaae1f0

Please sign in to comment.