Skip to content

Commit

Permalink
feat: Add support for controlplane rollout if ServerConfig has change…
Browse files Browse the repository at this point in the history
…d, mirroring the approach used by kubeadm

Signed-off-by: Richard Draycott <[email protected]>
  • Loading branch information
wikoion committed May 14, 2024
1 parent 0527d86 commit ed4a401
Showing 1 changed file with 51 additions and 0 deletions.
51 changes: 51 additions & 0 deletions pkg/machinefilters/machine_filters.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ limitations under the License.
package machinefilters

import (
"encoding/json"
"reflect"

"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1"
"sigs.k8s.io/cluster-api/util/collections"
Expand Down Expand Up @@ -83,6 +86,54 @@ func MatchesKubernetesVersion(kubernetesVersion string) Func {
// MatchesKThreesBootstrapConfig checks if machine's KThreesConfigSpec is equivalent with KCP's KThreesConfigSpec.
func MatchesKThreesBootstrapConfig(machineConfigs map[string]*bootstrapv1.KThreesConfig, kcp *controlplanev1.KThreesControlPlane) Func {
return func(machine *clusterv1.Machine) bool {
if machine == nil {
return false
}

// Check if KCP and machine KThreesServerConfig matches, if not return
if !matchKThreesServerConfig(kcp, machine) {
return false
}

bootstrapRef := machine.Spec.Bootstrap.ConfigRef
if bootstrapRef == nil {
// Missing bootstrap reference should not be considered as unmatching.
// This is a safety precaution to avoid selecting machines that are broken, which in the future should be remediated separately.
return true
}

return true
}
}

// matchKThreesServerConfig verifies if KCP and machine ClusterConfiguration matches.
// NOTE: Machines that have KThreesServerConfigurationAnnotation will have to match with KCP KThreesServerConfig.
// If the annotation is not present (machine is either old or adopted), we won't roll out on any possible changes
// made in KCP's KThreesServerConfig given that we don't have enough information to make a decision.
// Users should use KCP.Spec.RolloutAfter field to force a rollout in this case.
func matchKThreesServerConfig(kcp *controlplanev1.KThreesControlPlane, machine *clusterv1.Machine) bool {
kThreesServerConfigStr, ok := machine.GetAnnotations()[controlplanev1.KThreesServerConfigurationAnnotation]
if !ok {
// We don't have enough information to make a decision; don't' trigger a roll out.
return true
}

kThreesServerConfig := &bootstrapv1.KThreesServerConfig{}

// ClusterConfiguration annotation is not correct, only solution is to rollout.
// The call to json.Unmarshal has to take a pointer to the pointer struct defined above,
// otherwise we won't be able to handle a nil ClusterConfiguration (that is serialized into "null").
if err := json.Unmarshal([]byte(kThreesServerConfigStr), &kThreesServerConfig); err != nil {
return false
}

// If any of the compared values are nil, treat them the same as an empty KThreesServerConfig.
if kThreesServerConfig == nil {
kThreesServerConfig = &bootstrapv1.KThreesServerConfig{}
}

kcpLocalKThreesServerConfig := kcp.Spec.KThreesConfigSpec.ServerConfig

// Compare and return.
return reflect.DeepEqual(kThreesServerConfig, kcpLocalKThreesServerConfig)
}

0 comments on commit ed4a401

Please sign in to comment.