Skip to content

Commit

Permalink
Validate config apiVersion and kind
Browse files Browse the repository at this point in the history
Signed-off-by: Kimmo Lehto <klehto@mirantis.com>

Fix

Signed-off-by: Kimmo Lehto <klehto@mirantis.com>

Perform validation in Validate()

Signed-off-by: Kimmo Lehto <klehto@mirantis.com>
  • Loading branch information
kke committed Nov 25, 2021
1 parent 4fdd052 commit 7eb56a6
Show file tree
Hide file tree
Showing 10 changed files with 41 additions and 15 deletions.
4 changes: 2 additions & 2 deletions docs/airgap-install.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ As an alternative to the previous step, you can use k0sctl to upload the bundle

```YAML
apiVersion: k0sctl.k0sproject.io/v1beta1
kind: Cluster
kind: ClusterConfig
metadata:
name: k0s-cluster
spec:
Expand Down Expand Up @@ -93,7 +93,7 @@ Use the following `k0s.yaml` to ensure that containerd does not pull images for

```yaml
apiVersion: k0s.k0sproject.io/v1beta1
kind: Cluster
kind: ClusterConfig
metadata:
name: k0s
spec:
Expand Down
2 changes: 1 addition & 1 deletion docs/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ A YAML config file follows, with defaults as generated by the `k0s default-confi

```yaml
apiVersion: k0s.k0sproject.io/v1beta1
kind: Cluster
kind: ClusterConfig
metadata:
name: k0s
spec:
Expand Down
2 changes: 1 addition & 1 deletion docs/k0s-in-docker.md
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ services:
environment:
K0S_CONFIG: |-
apiVersion: k0s.k0sproject.io/v1beta1
kind: Cluster
kind: ClusterConfig
metadata:
name: k0s
# Any additional configuration goes here ...
Expand Down
2 changes: 1 addition & 1 deletion examples/footloose-ha-controllers/k0s.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
apiVersion: k0s.k0sproject.io/v1beta1
kind: Cluster
kind: ClusterConfig
metadata:
name: k0s-etcd
spec:
Expand Down
4 changes: 2 additions & 2 deletions examples/footloose-mysql/mke.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@

apiVersion: k0s.k0sproject.io/v1beta1
kind: Cluster
kind: ClusterConfig
metadata:
name: foobar
spec:
Expand All @@ -10,6 +10,6 @@ spec:
dataSource: mysql://root:kine@tcp(172.17.0.4)/kine
api:
address: 172.17.0.2 # Address where the k8s API is accessed at (nodes public IP or LB)



2 changes: 1 addition & 1 deletion inttest/backup/backup_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ import (

const configWithExternaladdress = `
apiVersion: k0s.k0sproject.io/v1beta1
kind: Cluster
kind: ClusterConfig
metadata:
name: k0s
spec:
Expand Down
2 changes: 1 addition & 1 deletion inttest/customports/customports_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ type Suite struct {

const configWithExternaladdress = `
apiVersion: k0s.k0sproject.io/v1beta1
kind: Cluster
kind: ClusterConfig
metadata:
name: k0s
spec:
Expand Down
17 changes: 12 additions & 5 deletions pkg/apis/k0s.k0sproject.io/v1beta1/clusterconfig_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -188,9 +188,11 @@ func ConfigFromString(yml string, dataDir string) (*ClusterConfig, error) {
if err != nil {
return config, err
}

if config.Spec == nil {
config.Spec = DefaultClusterSpec(dataDir)
}

return config, nil
}

Expand All @@ -199,18 +201,15 @@ func DefaultClusterConfig(dataDir string) *ClusterConfig {
return &ClusterConfig{
ObjectMeta: metav1.ObjectMeta{Name: "k0s"},
TypeMeta: metav1.TypeMeta{
APIVersion: "k0s.k0sproject.io/v1beta1",
Kind: "ClusterConfig",
APIVersion: ClusterConfigAPIVersion,
Kind: ClusterConfigKind,
},
Spec: DefaultClusterSpec(dataDir),
}
}

// UnmarshalJSON sets in some sane defaults when unmarshaling the data from json
func (c *ClusterConfig) UnmarshalJSON(data []byte) error {
if c.Kind == "" {
c.Kind = "ClusterConfig"
}
if c.ClusterName == "" {
c.ClusterName = "k0s"
}
Expand Down Expand Up @@ -268,6 +267,14 @@ type Validateable interface {
func (c *ClusterConfig) Validate() []error {
var errors []error

if c.APIVersion != ClusterConfigAPIVersion {
errors = append(errors, fmt.Errorf("expected apiVersion: %s but found %s", ClusterConfigAPIVersion, c.APIVersion))
}

if c.Kind != ClusterConfigKind {
errors = append(errors, fmt.Errorf("expected kind: %s but found %s", ClusterConfigKind, c.Kind))
}

errors = append(errors, validateSpecs(c.Spec.API)...)
errors = append(errors, validateSpecs(c.Spec.ControllerManager)...)
errors = append(errors, validateSpecs(c.Spec.Scheduler)...)
Expand Down
19 changes: 19 additions & 0 deletions pkg/apis/k0s.k0sproject.io/v1beta1/clusterconfig_types_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ package v1beta1

import (
"encoding/json"
"fmt"
"testing"

"github.com/k0sproject/k0s/internal/pkg/iface"
Expand All @@ -40,6 +41,24 @@ unknown: 1`, dataDir)
assert.Error(t, err)
}

func TestAPIVersionValidation(t *testing.T) {
c, _ := ConfigFromString("apiVersion: "+ClusterConfigAPIVersion, dataDir)
assert.Len(t, c.Validate(), 0)
c, _ = ConfigFromString("apiVersion: k0sctl.k0sproject.io/v1beta1", dataDir)
errors := c.Validate()
assert.Len(t, errors, 1)
assert.Contains(t, errors[0].Error(), "k0sctl.k0sproject.io")
}

func TestKindValidation(t *testing.T) {
c, _ := ConfigFromString(fmt.Sprintf("apiVersion: %s\nkind: %s", ClusterConfigAPIVersion, ClusterConfigKind), dataDir)
assert.Len(t, c.Validate(), 0)
c, _ = ConfigFromString(fmt.Sprintf("apiVersion: %s\nkind: %s", ClusterConfigAPIVersion, "UnsupportedKind"), dataDir)
errors := c.Validate()
assert.Len(t, errors, 1)
assert.Contains(t, errors[0].Error(), "UnsupportedKind")
}

func TestStorageDefaults(t *testing.T) {
yamlData := `
apiVersion: k0s.k0sproject.io/v1beta1
Expand Down
2 changes: 1 addition & 1 deletion pkg/apis/k0s.k0sproject.io/v1beta1/network_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ func (s *NetworkSuite) TestNetworkDefaults() {
func (s *NetworkSuite) TestCalicoDefaultsAfterMashaling() {
yamlData := `
apiVersion: k0s.k0sproject.io/v1beta1
kind: Cluster
kind: ClusterConfig
metadata:
name: foobar
spec:
Expand Down

0 comments on commit 7eb56a6

Please sign in to comment.