Skip to content

Commit 6b623d2

Browse files
authored
Merge pull request #566 from jnummelin/fix/use-k0s-image-for-init
Use same k0s image for certs init container
2 parents 6bbb963 + 964d6a2 commit 6b623d2

File tree

4 files changed

+100
-17
lines changed

4 files changed

+100
-17
lines changed

api/k0smotron.io/v1beta1/k0smotroncluster_types.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ package v1beta1
1818

1919
import (
2020
"fmt"
21+
"strings"
2122

2223
v1 "k8s.io/api/core/v1"
2324
"k8s.io/apimachinery/pkg/api/resource"
@@ -100,6 +101,29 @@ type ClusterSpec struct {
100101
Resources v1.ResourceRequirements `json:"resources,omitempty"`
101102
}
102103

104+
const (
105+
defaultK0SImage = "k0sproject/k0s"
106+
defaultK0SVersion = "v1.27.9-k0s.0"
107+
defaultK0SSuffix = "k0s.0"
108+
)
109+
110+
func (c *ClusterSpec) GetImage() string {
111+
k0sVersion := c.Version
112+
if k0sVersion == "" {
113+
k0sVersion = defaultK0SVersion
114+
}
115+
116+
if !strings.Contains(k0sVersion, "-k0s.") {
117+
k0sVersion = fmt.Sprintf("%s-%s", k0sVersion, defaultK0SSuffix)
118+
}
119+
120+
if c.Image == "" {
121+
return fmt.Sprintf("%s:%s", defaultK0SImage, k0sVersion)
122+
}
123+
124+
return fmt.Sprintf("%s:%s", c.Image, k0sVersion)
125+
}
126+
103127
// ClusterStatus defines the observed state of K0smotronCluster
104128
type ClusterStatus struct {
105129
ReconciliationStatus string `json:"reconciliationStatus"`
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
/*
2+
Copyright 2023.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package v1beta1
18+
19+
import (
20+
"testing"
21+
22+
"github.com/stretchr/testify/require"
23+
)
24+
25+
func TestClusterSpec_GetImage(t *testing.T) {
26+
27+
tests := []struct {
28+
name string
29+
spec *ClusterSpec
30+
want string
31+
}{
32+
{
33+
name: "Nothing given",
34+
spec: &ClusterSpec{},
35+
want: "k0sproject/k0s:v1.27.9-k0s.0",
36+
},
37+
{
38+
name: "Only version given with suffix",
39+
spec: &ClusterSpec{
40+
Version: "v1.29.4-k0s.0",
41+
},
42+
want: "k0sproject/k0s:v1.29.4-k0s.0",
43+
},
44+
{
45+
name: "Version given without suffix",
46+
spec: &ClusterSpec{
47+
Version: "v1.29.4",
48+
},
49+
want: "k0sproject/k0s:v1.29.4-k0s.0",
50+
},
51+
{
52+
name: "Image given without version should use default version",
53+
spec: &ClusterSpec{
54+
Image: "foobar/k0s",
55+
},
56+
want: "foobar/k0s:v1.27.9-k0s.0",
57+
},
58+
{
59+
name: "Image and version given",
60+
spec: &ClusterSpec{
61+
Image: "foobar/k0s",
62+
Version: "v1.29.4",
63+
},
64+
want: "foobar/k0s:v1.29.4-k0s.0",
65+
},
66+
}
67+
for _, tt := range tests {
68+
t.Run(tt.name, func(t *testing.T) {
69+
if got := tt.spec.GetImage(); got != tt.want {
70+
require.Equal(t, tt.want, got)
71+
}
72+
})
73+
}
74+
}

internal/controller/k0smotron.io/k0smotroncluster_controller.go

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -36,12 +36,6 @@ import (
3636
km "github.com/k0sproject/k0smotron/api/k0smotron.io/v1beta1"
3737
)
3838

39-
const (
40-
defaultK0SImage = "k0sproject/k0s"
41-
defaultK0SVersion = "v1.27.9-k0s.0"
42-
defaultK0SSuffix = "k0s.0"
43-
)
44-
4539
var patchOpts []client.PatchOption = []client.PatchOption{
4640
client.FieldOwner("k0smotron-operator"),
4741
client.ForceOwnership,

internal/controller/k0smotron.io/k0smotroncluster_statefulset.go

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ import (
2020
"context"
2121
"fmt"
2222
"reflect"
23-
"strings"
2423

2524
km "github.com/k0sproject/k0smotron/api/k0smotron.io/v1beta1"
2625
"github.com/k0sproject/k0smotron/internal/controller/util"
@@ -49,14 +48,6 @@ func (r *ClusterReconciler) findStatefulSetPod(ctx context.Context, statefulSet
4948
}
5049

5150
func (r *ClusterReconciler) generateStatefulSet(kmc *km.Cluster) (apps.StatefulSet, error) {
52-
k0sVersion := kmc.Spec.Version
53-
if k0sVersion == "" {
54-
k0sVersion = defaultK0SVersion
55-
} else {
56-
if kmc.Spec.Image == defaultK0SImage && !strings.Contains(kmc.Spec.Version, "-k0s.") {
57-
k0sVersion = fmt.Sprintf("%s-%s", kmc.Spec.Version, defaultK0SSuffix)
58-
}
59-
}
6051

6152
labels := labelsForCluster(kmc)
6253

@@ -120,7 +111,7 @@ func (r *ClusterReconciler) generateStatefulSet(kmc *km.Cluster) (apps.StatefulS
120111
}},
121112
Containers: []v1.Container{{
122113
Name: "controller",
123-
Image: fmt.Sprintf("%s:%s", kmc.Spec.Image, k0sVersion),
114+
Image: kmc.Spec.GetImage(),
124115
ImagePullPolicy: v1.PullIfNotPresent,
125116
Args: []string{"/k0smotron-entrypoint.sh"},
126117
Ports: []v1.ContainerPort{
@@ -403,7 +394,7 @@ func (r *ClusterReconciler) mountSecrets(kmc *km.Cluster, sfs *apps.StatefulSet)
403394
// Otherwise k0s will trip over the permissions and RO mounts
404395
sfs.Spec.Template.Spec.InitContainers = append(sfs.Spec.Template.Spec.InitContainers, v1.Container{
405396
Name: "certs-init",
406-
Image: "busybox",
397+
Image: kmc.Spec.GetImage(),
407398
Command: []string{
408399
"sh",
409400
"-c",

0 commit comments

Comments
 (0)