Skip to content

Commit

Permalink
Merge pull request #566 from jnummelin/fix/use-k0s-image-for-init
Browse files Browse the repository at this point in the history
Use same k0s image for certs init container
  • Loading branch information
jnummelin authored May 10, 2024
2 parents 6bbb963 + 964d6a2 commit 6b623d2
Show file tree
Hide file tree
Showing 4 changed files with 100 additions and 17 deletions.
24 changes: 24 additions & 0 deletions api/k0smotron.io/v1beta1/k0smotroncluster_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package v1beta1

import (
"fmt"
"strings"

v1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/resource"
Expand Down Expand Up @@ -100,6 +101,29 @@ type ClusterSpec struct {
Resources v1.ResourceRequirements `json:"resources,omitempty"`
}

const (
defaultK0SImage = "k0sproject/k0s"
defaultK0SVersion = "v1.27.9-k0s.0"
defaultK0SSuffix = "k0s.0"
)

func (c *ClusterSpec) GetImage() string {
k0sVersion := c.Version
if k0sVersion == "" {
k0sVersion = defaultK0SVersion
}

if !strings.Contains(k0sVersion, "-k0s.") {
k0sVersion = fmt.Sprintf("%s-%s", k0sVersion, defaultK0SSuffix)
}

if c.Image == "" {
return fmt.Sprintf("%s:%s", defaultK0SImage, k0sVersion)
}

return fmt.Sprintf("%s:%s", c.Image, k0sVersion)
}

// ClusterStatus defines the observed state of K0smotronCluster
type ClusterStatus struct {
ReconciliationStatus string `json:"reconciliationStatus"`
Expand Down
74 changes: 74 additions & 0 deletions api/k0smotron.io/v1beta1/k0smotroncluster_types_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
/*
Copyright 2023.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package v1beta1

import (
"testing"

"github.com/stretchr/testify/require"
)

func TestClusterSpec_GetImage(t *testing.T) {

tests := []struct {
name string
spec *ClusterSpec
want string
}{
{
name: "Nothing given",
spec: &ClusterSpec{},
want: "k0sproject/k0s:v1.27.9-k0s.0",
},
{
name: "Only version given with suffix",
spec: &ClusterSpec{
Version: "v1.29.4-k0s.0",
},
want: "k0sproject/k0s:v1.29.4-k0s.0",
},
{
name: "Version given without suffix",
spec: &ClusterSpec{
Version: "v1.29.4",
},
want: "k0sproject/k0s:v1.29.4-k0s.0",
},
{
name: "Image given without version should use default version",
spec: &ClusterSpec{
Image: "foobar/k0s",
},
want: "foobar/k0s:v1.27.9-k0s.0",
},
{
name: "Image and version given",
spec: &ClusterSpec{
Image: "foobar/k0s",
Version: "v1.29.4",
},
want: "foobar/k0s:v1.29.4-k0s.0",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := tt.spec.GetImage(); got != tt.want {
require.Equal(t, tt.want, got)
}
})
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,6 @@ import (
km "github.com/k0sproject/k0smotron/api/k0smotron.io/v1beta1"
)

const (
defaultK0SImage = "k0sproject/k0s"
defaultK0SVersion = "v1.27.9-k0s.0"
defaultK0SSuffix = "k0s.0"
)

var patchOpts []client.PatchOption = []client.PatchOption{
client.FieldOwner("k0smotron-operator"),
client.ForceOwnership,
Expand Down
13 changes: 2 additions & 11 deletions internal/controller/k0smotron.io/k0smotroncluster_statefulset.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import (
"context"
"fmt"
"reflect"
"strings"

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

func (r *ClusterReconciler) generateStatefulSet(kmc *km.Cluster) (apps.StatefulSet, error) {
k0sVersion := kmc.Spec.Version
if k0sVersion == "" {
k0sVersion = defaultK0SVersion
} else {
if kmc.Spec.Image == defaultK0SImage && !strings.Contains(kmc.Spec.Version, "-k0s.") {
k0sVersion = fmt.Sprintf("%s-%s", kmc.Spec.Version, defaultK0SSuffix)
}
}

labels := labelsForCluster(kmc)

Expand Down Expand Up @@ -120,7 +111,7 @@ func (r *ClusterReconciler) generateStatefulSet(kmc *km.Cluster) (apps.StatefulS
}},
Containers: []v1.Container{{
Name: "controller",
Image: fmt.Sprintf("%s:%s", kmc.Spec.Image, k0sVersion),
Image: kmc.Spec.GetImage(),
ImagePullPolicy: v1.PullIfNotPresent,
Args: []string{"/k0smotron-entrypoint.sh"},
Ports: []v1.ContainerPort{
Expand Down Expand Up @@ -403,7 +394,7 @@ func (r *ClusterReconciler) mountSecrets(kmc *km.Cluster, sfs *apps.StatefulSet)
// Otherwise k0s will trip over the permissions and RO mounts
sfs.Spec.Template.Spec.InitContainers = append(sfs.Spec.Template.Spec.InitContainers, v1.Container{
Name: "certs-init",
Image: "busybox",
Image: kmc.Spec.GetImage(),
Command: []string{
"sh",
"-c",
Expand Down

0 comments on commit 6b623d2

Please sign in to comment.