-
Notifications
You must be signed in to change notification settings - Fork 38
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
feat: Changes to controlplane spec should trigger a rollout #114
Merged
mogliang
merged 5 commits into
k3s-io:main
from
wikoion:wikoion/rollout-after-spec-change
May 20, 2024
Merged
Changes from 3 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
c451faa
feat: Add support for controlplane rollout if ServerConfig has change…
wikoion 9b1f81b
feat: Add unit tests and add matching logic for the rest of kthreesco…
wikoion 81d4839
feat: Add more machinefilter test cases and exclude version from check
wikoion 0550197
feat: Fix test name
wikoion 9caad96
feat: Simply code by removing redundant serverConfig check via annota…
wikoion File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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,283 @@ | ||
package machinefilters | ||
|
||
import ( | ||
"testing" | ||
|
||
. "github.com/onsi/gomega" | ||
"google.golang.org/protobuf/proto" | ||
corev1 "k8s.io/api/core/v1" | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1" | ||
|
||
bootstrapv1 "github.com/k3s-io/cluster-api-k3s/bootstrap/api/v1beta2" | ||
controlplanev1 "github.com/k3s-io/cluster-api-k3s/controlplane/api/v1beta2" | ||
) | ||
|
||
func TestMatchesKubeadmBootstrapConfig(t *testing.T) { | ||
t.Run("returns true if ClusterConfiguration is equal", func(t *testing.T) { | ||
g := NewWithT(t) | ||
kcp := &controlplanev1.KThreesControlPlane{ | ||
Spec: controlplanev1.KThreesControlPlaneSpec{ | ||
KThreesConfigSpec: bootstrapv1.KThreesConfigSpec{ | ||
ServerConfig: bootstrapv1.KThreesServerConfig{ | ||
ClusterDomain: "foo", | ||
}, | ||
}, | ||
}, | ||
} | ||
m := &clusterv1.Machine{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Annotations: map[string]string{ | ||
controlplanev1.KThreesServerConfigurationAnnotation: "{\n \"clusterDomain\": \"foo\"\n}", | ||
}, | ||
}, | ||
} | ||
machineConfigs := map[string]*bootstrapv1.KThreesConfig{ | ||
m.Name: {}, | ||
} | ||
match := MatchesKThreesBootstrapConfig(machineConfigs, kcp)(m) | ||
g.Expect(match).To(BeTrue()) | ||
}) | ||
t.Run("returns false if ClusterConfiguration is NOT equal", func(t *testing.T) { | ||
g := NewWithT(t) | ||
kcp := &controlplanev1.KThreesControlPlane{ | ||
Spec: controlplanev1.KThreesControlPlaneSpec{ | ||
KThreesConfigSpec: bootstrapv1.KThreesConfigSpec{ | ||
ServerConfig: bootstrapv1.KThreesServerConfig{ | ||
ClusterDomain: "foo", | ||
}, | ||
}, | ||
}, | ||
} | ||
m := &clusterv1.Machine{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Annotations: map[string]string{ | ||
controlplanev1.KThreesServerConfigurationAnnotation: "{\n \"clusterDomain\": \"bar\"\n}", | ||
}, | ||
}, | ||
} | ||
machineConfigs := map[string]*bootstrapv1.KThreesConfig{ | ||
m.Name: {}, | ||
} | ||
match := MatchesKThreesBootstrapConfig(machineConfigs, kcp)(m) | ||
g.Expect(match).To(BeFalse()) | ||
}) | ||
|
||
t.Run("returns false if some other configurations are not equal", func(t *testing.T) { | ||
g := NewWithT(t) | ||
kcp := &controlplanev1.KThreesControlPlane{ | ||
Spec: controlplanev1.KThreesControlPlaneSpec{ | ||
KThreesConfigSpec: bootstrapv1.KThreesConfigSpec{ | ||
Files: []bootstrapv1.File{}, // This is a change | ||
}, | ||
}, | ||
} | ||
|
||
m := &clusterv1.Machine{ | ||
TypeMeta: metav1.TypeMeta{ | ||
Kind: "KThreesConfig", | ||
APIVersion: clusterv1.GroupVersion.String(), | ||
}, | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Namespace: "default", | ||
Name: "test", | ||
}, | ||
Spec: clusterv1.MachineSpec{ | ||
Bootstrap: clusterv1.Bootstrap{ | ||
ConfigRef: &corev1.ObjectReference{ | ||
Kind: "KThreesConfig", | ||
Namespace: "default", | ||
Name: "test", | ||
APIVersion: bootstrapv1.GroupVersion.String(), | ||
}, | ||
}, | ||
}, | ||
} | ||
machineConfigs := map[string]*bootstrapv1.KThreesConfig{ | ||
m.Name: { | ||
TypeMeta: metav1.TypeMeta{ | ||
Kind: "KThreesConfig", | ||
APIVersion: bootstrapv1.GroupVersion.String(), | ||
}, | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Namespace: "default", | ||
Name: "test", | ||
}, | ||
Spec: bootstrapv1.KThreesConfigSpec{}, | ||
}, | ||
} | ||
match := MatchesKThreesBootstrapConfig(machineConfigs, kcp)(m) | ||
g.Expect(match).To(BeFalse()) | ||
}) | ||
|
||
t.Run("Should match on other configurations", func(t *testing.T) { | ||
kThreesConfigSpec := bootstrapv1.KThreesConfigSpec{ | ||
Files: []bootstrapv1.File{}, | ||
PreK3sCommands: []string{"test"}, | ||
PostK3sCommands: []string{"test"}, | ||
AgentConfig: bootstrapv1.KThreesAgentConfig{ | ||
NodeName: "test-node", | ||
NodeTaints: []string{"node-role.kubernetes.io/control-plane:NoSchedule"}, | ||
KubeProxyArgs: []string{"metrics-bind-address=0.0.0.0"}, | ||
}, | ||
ServerConfig: bootstrapv1.KThreesServerConfig{ | ||
DisableComponents: []string{"traefik"}, | ||
KubeControllerManagerArgs: []string{"bind-address=0.0.0.0"}, | ||
KubeSchedulerArgs: []string{"bind-address=0.0.0.0"}, | ||
}, | ||
} | ||
kcp := &controlplanev1.KThreesControlPlane{ | ||
Spec: controlplanev1.KThreesControlPlaneSpec{ | ||
Replicas: proto.Int32(3), | ||
Version: "v1.13.14+k3s1", | ||
MachineTemplate: controlplanev1.KThreesControlPlaneMachineTemplate{ | ||
ObjectMeta: clusterv1.ObjectMeta{ | ||
Labels: map[string]string{"test-label": "test-value"}, | ||
}, | ||
}, | ||
KThreesConfigSpec: kThreesConfigSpec, | ||
}, | ||
} | ||
|
||
m := &clusterv1.Machine{ | ||
TypeMeta: metav1.TypeMeta{ | ||
Kind: "KThreesConfig", | ||
APIVersion: clusterv1.GroupVersion.String(), | ||
}, | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Namespace: "default", | ||
Name: "test", | ||
}, | ||
Spec: clusterv1.MachineSpec{ | ||
Bootstrap: clusterv1.Bootstrap{ | ||
ConfigRef: &corev1.ObjectReference{ | ||
Kind: "KThreesConfig", | ||
Namespace: "default", | ||
Name: "test", | ||
APIVersion: bootstrapv1.GroupVersion.String(), | ||
}, | ||
}, | ||
}, | ||
} | ||
machineConfigs := map[string]*bootstrapv1.KThreesConfig{ | ||
m.Name: { | ||
TypeMeta: metav1.TypeMeta{ | ||
Kind: "KThreesConfig", | ||
APIVersion: bootstrapv1.GroupVersion.String(), | ||
}, | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Namespace: "default", | ||
Name: "test", | ||
}, | ||
Spec: kThreesConfigSpec, | ||
}, | ||
} | ||
|
||
t.Run("by returning true if all configs match", func(t *testing.T) { | ||
g := NewWithT(t) | ||
match := MatchesKThreesBootstrapConfig(machineConfigs, kcp)(m) | ||
g.Expect(match).To(BeTrue()) | ||
}) | ||
|
||
t.Run("by returning false if post commands don't match", func(t *testing.T) { | ||
g := NewWithT(t) | ||
machineConfigs[m.Name].Spec.PostK3sCommands = []string{"new-test"} | ||
match := MatchesKThreesBootstrapConfig(machineConfigs, kcp)(m) | ||
g.Expect(match).To(BeFalse()) | ||
}) | ||
|
||
t.Run("by returning false if agent configs don't match", func(t *testing.T) { | ||
g := NewWithT(t) | ||
machineConfigs[m.Name].Spec.AgentConfig.KubeletArgs = []string{"test-arg"} | ||
match := MatchesKThreesBootstrapConfig(machineConfigs, kcp)(m) | ||
g.Expect(match).To(BeFalse()) | ||
}) | ||
}) | ||
|
||
t.Run("should match on labels and annotations", func(t *testing.T) { | ||
kcp := &controlplanev1.KThreesControlPlane{ | ||
Spec: controlplanev1.KThreesControlPlaneSpec{ | ||
MachineTemplate: controlplanev1.KThreesControlPlaneMachineTemplate{ | ||
ObjectMeta: clusterv1.ObjectMeta{ | ||
Annotations: map[string]string{ | ||
"test": "annotation", | ||
}, | ||
Labels: map[string]string{ | ||
"test": "labels", | ||
}, | ||
}, | ||
}, | ||
KThreesConfigSpec: bootstrapv1.KThreesConfigSpec{ | ||
ServerConfig: bootstrapv1.KThreesServerConfig{}, | ||
}, | ||
}, | ||
} | ||
m := &clusterv1.Machine{ | ||
TypeMeta: metav1.TypeMeta{ | ||
Kind: "KThreesConfig", | ||
APIVersion: clusterv1.GroupVersion.String(), | ||
}, | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Namespace: "default", | ||
Name: "test", | ||
}, | ||
Spec: clusterv1.MachineSpec{ | ||
Bootstrap: clusterv1.Bootstrap{ | ||
ConfigRef: &corev1.ObjectReference{ | ||
Kind: "KThreesConfig", | ||
Namespace: "default", | ||
Name: "test", | ||
APIVersion: bootstrapv1.GroupVersion.String(), | ||
}, | ||
}, | ||
}, | ||
} | ||
machineConfigs := map[string]*bootstrapv1.KThreesConfig{ | ||
m.Name: { | ||
TypeMeta: metav1.TypeMeta{ | ||
Kind: "KThreesConfig", | ||
APIVersion: bootstrapv1.GroupVersion.String(), | ||
}, | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Namespace: "default", | ||
Name: "test", | ||
}, | ||
Spec: bootstrapv1.KThreesConfigSpec{ | ||
ServerConfig: bootstrapv1.KThreesServerConfig{}, | ||
}, | ||
}, | ||
} | ||
|
||
t.Run("by returning true if neither labels or annotations match", func(t *testing.T) { | ||
g := NewWithT(t) | ||
machineConfigs[m.Name].Annotations = nil | ||
machineConfigs[m.Name].Labels = nil | ||
match := MatchesKThreesBootstrapConfig(machineConfigs, kcp)(m) | ||
g.Expect(match).To(BeTrue()) | ||
}) | ||
|
||
t.Run("by returning true if only labels don't match", func(t *testing.T) { | ||
g := NewWithT(t) | ||
machineConfigs[m.Name].Annotations = kcp.Spec.MachineTemplate.ObjectMeta.Annotations | ||
machineConfigs[m.Name].Labels = nil | ||
match := MatchesKThreesBootstrapConfig(machineConfigs, kcp)(m) | ||
g.Expect(match).To(BeTrue()) | ||
}) | ||
|
||
t.Run("by returning true if only annotations don't match", func(t *testing.T) { | ||
g := NewWithT(t) | ||
machineConfigs[m.Name].Annotations = nil | ||
machineConfigs[m.Name].Labels = kcp.Spec.MachineTemplate.ObjectMeta.Labels | ||
match := MatchesKThreesBootstrapConfig(machineConfigs, kcp)(m) | ||
g.Expect(match).To(BeTrue()) | ||
}) | ||
|
||
t.Run("by returning true if both labels and annotations match", func(t *testing.T) { | ||
g := NewWithT(t) | ||
machineConfigs[m.Name].Labels = kcp.Spec.MachineTemplate.ObjectMeta.Labels | ||
machineConfigs[m.Name].Annotations = kcp.Spec.MachineTemplate.ObjectMeta.Annotations | ||
match := MatchesKThreesBootstrapConfig(machineConfigs, kcp)(m) | ||
g.Expect(match).To(BeTrue()) | ||
}) | ||
}) | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
at least for controlplane, init and join cp node has same bootstrap config, this is different from how kubeadm works
cluster-api-k3s/pkg/k3s/control_plane.go
Lines 191 to 201 in 2a14b6e
I'm not very sure about worker node, you may provision a k3s cluster and check the bootstrap config.
Let's try if we can simplify this code
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah, I see what you mean. The init and join config is just the kcp KThreesConfigSpec, we do still compare this here, how do you suggest we should simplify the code?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
we can simply compare kcp config and machine bootstrap config directly, no need to compare serverConfig separately
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Addressed in the latest commit