Skip to content

Commit

Permalink
Merge pull request #3377 from jnummelin/rel-1.25/k0s-crd-fix
Browse files Browse the repository at this point in the history
[Release 1.25] k0s crd fix
  • Loading branch information
jnummelin authored Aug 14, 2023
2 parents 8d3284b + bfd1f07 commit ee57e27
Show file tree
Hide file tree
Showing 8 changed files with 48 additions and 23 deletions.
26 changes: 23 additions & 3 deletions inttest/configchange/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,23 @@ func TestConfigSuite(t *testing.T) {
suite.Run(t, &s)
}

func (s *ConfigSuite) TestK0sGetsUp() {
var config = `
apiVersion: k0s.k0sproject.io/v1beta1
kind: ClusterConfig
metadata:
name: k0s
spec:
workerProfiles:
- name: limit-pods
values:
maxPods: 20
`

s.NoError(s.InitController(0, "--enable-dynamic-config"))
s.NoError(s.RunWorkers())
func (s *ConfigSuite) TestK0sGetsUp() {
s.PutFile(s.ControllerNode(0), "/tmp/k0s.yaml", config)
s.NoError(s.InitController(0, "--enable-dynamic-config", "--config /tmp/k0s.yaml"))
s.NoError(s.RunWorkers("--profile limit-pods"))

kc, err := s.KubeClient(s.ControllerNode(0))
s.NoError(err)
Expand All @@ -64,6 +77,13 @@ func (s *ConfigSuite) TestK0sGetsUp() {
s.NoError(err)
err = s.WaitForNodeReady(s.WorkerNode(1), kc)
s.NoError(err)
// Check that the node capacity has only 20 pods
for i := range []int{0, 1} {
node, err := kc.CoreV1().Nodes().Get(s.Context(), s.WorkerNode(i), metav1.GetOptions{})
s.Require().NoError(err)
s.EqualValues(20, node.Status.Capacity.Pods().Value())
}

s.T().Log("waiting to see kube-router pods ready")
s.NoError(common.WaitForKubeRouterReady(kc), "kube-router did not start")

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@ spec:
j := c.Spec.WorkerProfiles[1].Config
var parsed map[string]interface{}

err = json.Unmarshal(j, &parsed)
err = json.Unmarshal(j.Raw, &parsed)
assert.NoError(t, err)

for field, value := range parsed {
Expand Down
10 changes: 7 additions & 3 deletions pkg/apis/k0s.k0sproject.io/v1beta1/workerprofile.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ package v1beta1
import (
"encoding/json"
"fmt"

"k8s.io/apimachinery/pkg/runtime"
)

var _ Validateable = (*WorkerProfiles)(nil)
Expand All @@ -42,7 +44,10 @@ type WorkerProfile struct {
// String; name to use as profile selector for the worker process
Name string `json:"name"`
// Worker Mapping object
Config json.RawMessage `json:"values"`
// +kubebuilder:validation:type=object
// +kubebuilder:pruning:PreserveUnknownFields
// +kubebuilder:validation:Optional
Config *runtime.RawExtension `json:"values"`
}

var lockedFields = map[string]struct{}{
Expand All @@ -56,8 +61,7 @@ var lockedFields = map[string]struct{}{
// Validate validates instance
func (wp *WorkerProfile) Validate() error {
var parsed map[string]interface{}

err := json.Unmarshal(wp.Config, &parsed)
err := json.Unmarshal(wp.Config.Raw, &parsed)
if err != nil {
return err
}
Expand Down
5 changes: 3 additions & 2 deletions pkg/apis/k0s.k0sproject.io/v1beta1/workerprofile_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"testing"

"github.com/stretchr/testify/assert"
"k8s.io/apimachinery/pkg/runtime"
)

// TestWorkerProfile worker profile test suite
Expand Down Expand Up @@ -74,11 +75,11 @@ func TestWorkerProfile(t *testing.T) {

for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
b, err := json.Marshal(tc.spec)
value, err := json.Marshal(tc.spec)
if err != nil {
t.Fatal(err)
}
profile := WorkerProfile{Config: b}
profile := WorkerProfile{Config: &runtime.RawExtension{Raw: value}}
valid := profile.Validate() == nil
assert.Equal(t, valid, tc.valid)
})
Expand Down
7 changes: 3 additions & 4 deletions pkg/apis/k0s.k0sproject.io/v1beta1/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion pkg/component/controller/kubeletconfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ func (k *KubeletConfig) createProfiles(clusterSpec *v1beta1.ClusterConfig) (*byt
profileConfig := getDefaultProfile(dnsAddress, clusterSpec.Spec.Network.ClusterDomain)

var workerValues unstructuredYamlObject
err := json.Unmarshal(profile.Config, &workerValues)
err := json.Unmarshal(profile.Config.Raw, &workerValues)
if err != nil {
return nil, fmt.Errorf("failed to decode worker profile values: %v", err)
}
Expand Down
15 changes: 8 additions & 7 deletions pkg/component/controller/kubeletconfig_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,9 @@ import (

"github.com/k0sproject/k0s/internal/testutil"
"github.com/k0sproject/k0s/pkg/apis/helm.k0sproject.io/v1beta1"
config "github.com/k0sproject/k0s/pkg/apis/k0s.k0sproject.io/v1beta1"
k0sv1beta1 "github.com/k0sproject/k0s/pkg/apis/k0s.k0sproject.io/v1beta1"
"github.com/k0sproject/k0s/pkg/constant"
"k8s.io/apimachinery/pkg/runtime"
"sigs.k8s.io/yaml"

"github.com/stretchr/testify/require"
Expand All @@ -33,7 +34,7 @@ import (
var k0sVars = constant.GetConfig("")

func Test_KubeletConfig(t *testing.T) {
cfg := config.DefaultClusterConfig()
cfg := k0sv1beta1.DefaultClusterConfig()
dnsAddr, _ := cfg.Spec.Network.DNSAddress()
t.Run("default_profile_only", func(t *testing.T) {
k := NewKubeletConfig(k0sVars, testutil.NewFakeClientFactory(), cfg)
Expand Down Expand Up @@ -118,7 +119,7 @@ func Test_KubeletConfig(t *testing.T) {
})
}

func defaultConfigWithUserProvidedProfiles(t *testing.T, cfg *config.ClusterConfig) *KubeletConfig {
func defaultConfigWithUserProvidedProfiles(t *testing.T, cfg *k0sv1beta1.ClusterConfig) *KubeletConfig {
k0sVars := constant.GetConfig(t.TempDir())
k := NewKubeletConfig(k0sVars, testutil.NewFakeClientFactory(), cfg)

Expand All @@ -134,9 +135,9 @@ func defaultConfigWithUserProvidedProfiles(t *testing.T, cfg *config.ClusterConf
t.Fatal(err)
}
cfg.Spec.WorkerProfiles = append(cfg.Spec.WorkerProfiles,
config.WorkerProfile{
k0sv1beta1.WorkerProfile{
Name: "profile_XXX",
Config: wcx,
Config: &runtime.RawExtension{Raw: wcx},
},
)

Expand All @@ -154,9 +155,9 @@ func defaultConfigWithUserProvidedProfiles(t *testing.T, cfg *config.ClusterConf
}

cfg.Spec.WorkerProfiles = append(cfg.Spec.WorkerProfiles,
config.WorkerProfile{
k0sv1beta1.WorkerProfile{
Name: "profile_YYY",
Config: wcy,
Config: &runtime.RawExtension{Raw: wcy},
},
)
return k
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -448,8 +448,8 @@ spec:
type: string
values:
description: Worker Mapping object
format: byte
type: string
type: object
x-kubernetes-preserve-unknown-fields: true
type: object
type: array
type: object
Expand Down

0 comments on commit ee57e27

Please sign in to comment.