-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #9 from replicatedhq/import-zone
Adding page rules to import
- Loading branch information
Showing
7 changed files
with
130 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |