Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support specifying control plane firewall rules when creating or updating DOKS clusters #696

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 21 additions & 12 deletions kubernetes.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,18 +76,20 @@ type KubernetesClusterCreateRequest struct {

NodePools []*KubernetesNodePoolCreateRequest `json:"node_pools,omitempty"`

MaintenancePolicy *KubernetesMaintenancePolicy `json:"maintenance_policy"`
AutoUpgrade bool `json:"auto_upgrade"`
SurgeUpgrade bool `json:"surge_upgrade"`
MaintenancePolicy *KubernetesMaintenancePolicy `json:"maintenance_policy"`
AutoUpgrade bool `json:"auto_upgrade"`
SurgeUpgrade bool `json:"surge_upgrade"`
ControlPlanePermission *KubernetesControlPlanePermission `json:"control_plane_permission,omitempty"`
}

// KubernetesClusterUpdateRequest represents a request to update a Kubernetes cluster.
type KubernetesClusterUpdateRequest struct {
Name string `json:"name,omitempty"`
Tags []string `json:"tags,omitempty"`
MaintenancePolicy *KubernetesMaintenancePolicy `json:"maintenance_policy,omitempty"`
AutoUpgrade *bool `json:"auto_upgrade,omitempty"`
SurgeUpgrade bool `json:"surge_upgrade,omitempty"`
Name string `json:"name,omitempty"`
Tags []string `json:"tags,omitempty"`
MaintenancePolicy *KubernetesMaintenancePolicy `json:"maintenance_policy,omitempty"`
AutoUpgrade *bool `json:"auto_upgrade,omitempty"`
SurgeUpgrade bool `json:"surge_upgrade,omitempty"`
ControlPlanePermission *KubernetesControlPlanePermission `json:"control_plane_permission,omitempty"`
Copy link
Contributor

@timoreimann timoreimann Jun 5, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sanity check: will omitempty semantics combined with the KubernetesControlPlanePermission type not being pointerized work for our case? That is, will be see the desired behavior / serialization when the field is omitted for a given cluster configuration (firewall disabled vs enabled)?

Genuinely asking because this part of the Go JSON library is always a bit scary, and mistakes can be very hard to correct once the API is in use.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi Timo, this is fine since I had been using my godo fork using this new struct for cluster that don't use control plane permission and e2e tests for clusters that use the feature. Also, we do have some logic if the field is not provided it won't be considered for storing or updating the record.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since Enabled takes a pointer and does not use omitempty, you should get the behavior (I'm assuming) you want.

Enabled          *bool    `json:"enabled"`

https://go.dev/play/p/nSgNNF_c1Lx


// Convert cluster to run highly available control plane
HA *bool `json:"ha,omitempty"`
Expand Down Expand Up @@ -201,10 +203,11 @@ type KubernetesCluster struct {

NodePools []*KubernetesNodePool `json:"node_pools,omitempty"`

MaintenancePolicy *KubernetesMaintenancePolicy `json:"maintenance_policy,omitempty"`
AutoUpgrade bool `json:"auto_upgrade,omitempty"`
SurgeUpgrade bool `json:"surge_upgrade,omitempty"`
RegistryEnabled bool `json:"registry_enabled,omitempty"`
MaintenancePolicy *KubernetesMaintenancePolicy `json:"maintenance_policy,omitempty"`
AutoUpgrade bool `json:"auto_upgrade,omitempty"`
SurgeUpgrade bool `json:"surge_upgrade,omitempty"`
RegistryEnabled bool `json:"registry_enabled,omitempty"`
ControlPlanePermission *KubernetesControlPlanePermission `json:"control_plane_permission,omitempty"`

Status *KubernetesClusterStatus `json:"status,omitempty"`
CreatedAt time.Time `json:"created_at,omitempty"`
Expand Down Expand Up @@ -240,6 +243,12 @@ type KubernetesMaintenancePolicy struct {
Day KubernetesMaintenancePolicyDay `json:"day"`
}

// KubernetesControlPlanePermission represents Kubernetes cluster control plane permission.
type KubernetesControlPlanePermission struct {
Enabled *bool `json:"enabled"`
AllowedAddresses []string `json:"allowed_addresses"`
}

// KubernetesMaintenancePolicyDay represents the possible days of a maintenance
// window
type KubernetesMaintenancePolicyDay int
Expand Down
38 changes: 35 additions & 3 deletions kubernetes_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -542,6 +542,7 @@ func TestKubernetesClusters_Create(t *testing.T) {
defer teardown()

kubeSvc := client.Kubernetes
enabled := true

want := &KubernetesCluster{
ID: "8d91899c-0739-4a1a-acc5-deadbeefbb8f",
Expand All @@ -568,6 +569,12 @@ func TestKubernetesClusters_Create(t *testing.T) {
StartTime: "00:00",
Day: KubernetesMaintenanceDayMonday,
},
ControlPlanePermission: &KubernetesControlPlanePermission{
Enabled: &enabled,
AllowedAddresses: []string{
"1.2.3.4/32",
},
},
}
createRequest := &KubernetesClusterCreateRequest{
Name: want.Name,
Expand Down Expand Up @@ -625,7 +632,13 @@ func TestKubernetesClusters_Create(t *testing.T) {
"maintenance_policy": {
"start_time": "00:00",
"day": "monday"
}
},
"control_plane_permission": {
"enabled": true,
"allowed_addresses": [
"1.2.3.4/32"
]
}
}
}`

Expand Down Expand Up @@ -755,6 +768,7 @@ func TestKubernetesClusters_Update(t *testing.T) {
defer teardown()

kubeSvc := client.Kubernetes
enabled := true

want := &KubernetesCluster{
ID: "8d91899c-0739-4a1a-acc5-deadbeefbb8f",
Expand Down Expand Up @@ -783,12 +797,24 @@ func TestKubernetesClusters_Update(t *testing.T) {
StartTime: "00:00",
Day: KubernetesMaintenanceDayMonday,
},
ControlPlanePermission: &KubernetesControlPlanePermission{
Enabled: &enabled,
AllowedAddresses: []string{
"1.2.3.4/32",
},
},
}
updateRequest := &KubernetesClusterUpdateRequest{
Name: want.Name,
Tags: want.Tags,
MaintenancePolicy: want.MaintenancePolicy,
SurgeUpgrade: true,
ControlPlanePermission: &KubernetesControlPlanePermission{
Enabled: &enabled,
AllowedAddresses: []string{
"1.2.3.4/32",
},
},
}

jBlob := `
Expand Down Expand Up @@ -824,11 +850,17 @@ func TestKubernetesClusters_Update(t *testing.T) {
"maintenance_policy": {
"start_time": "00:00",
"day": "monday"
}
},
"control_plane_permission": {
"enabled": true,
"allowed_addresses": [
"1.2.3.4/32"
]
}
}
}`

expectedReqJSON := `{"name":"antoine-test-cluster","tags":["cluster-tag-1","cluster-tag-2"],"maintenance_policy":{"start_time":"00:00","duration":"","day":"monday"},"surge_upgrade":true}
expectedReqJSON := `{"name":"antoine-test-cluster","tags":["cluster-tag-1","cluster-tag-2"],"maintenance_policy":{"start_time":"00:00","duration":"","day":"monday"},"surge_upgrade":true,"control_plane_permission":{"enabled":true,"allowed_addresses":["1.2.3.4/32"]}}
`

mux.HandleFunc("/v2/kubernetes/clusters/8d91899c-0739-4a1a-acc5-deadbeefbb8f", func(w http.ResponseWriter, r *http.Request) {
Expand Down
Loading