Skip to content
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

Validate config apiVersion and kind #1245

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 config create

```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 @@ -166,9 +166,11 @@ func ConfigFromString(yml string, defaultStorage ...*StorageSpec) (*ClusterConfi
if err != nil {
return config, err
}

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

return config, nil
}

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

// 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 @@ -263,6 +262,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 @@ -38,6 +39,24 @@ unknown: 1`)
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
6 changes: 3 additions & 3 deletions pkg/component/controller/clusterConfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import (
)

var (
resourceType = v1.TypeMeta{APIVersion: "k0s.k0sproject.io/v1beta1", Kind: "clusterconfigs"}
resourceType = v1.TypeMeta{APIVersion: "k0s.k0sproject.io/v1beta1", Kind: "ClusterConfig"}
cOpts = v1.CreateOptions{TypeMeta: resourceType}
getOpts = v1.GetOptions{TypeMeta: resourceType}
)
Expand Down Expand Up @@ -172,11 +172,11 @@ func (r *ClusterConfigReconciler) reportStatus(config *v1beta1.ClusterConfig, re
FirstTimestamp: v1.Now(),
LastTimestamp: v1.Now(),
InvolvedObject: corev1.ObjectReference{
Kind: v1beta1.ClusterConfigKind,
Kind: config.Kind,
Namespace: config.Namespace,
Name: config.Name,
UID: config.UID,
APIVersion: v1beta1.ClusterConfigAPIVersion,
APIVersion: config.APIVersion,
ResourceVersion: config.ResourceVersion,
},
Action: "ConfigReconciling",
Expand Down
2 changes: 1 addition & 1 deletion pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ import (
)

var (
resourceType = v1.TypeMeta{APIVersion: "k0s.k0sproject.io/v1beta1", Kind: "clusterconfigs"}
resourceType = v1.TypeMeta{APIVersion: "k0s.k0sproject.io/v1beta1", Kind: "ClusterConfig"}
getOpts = v1.GetOptions{TypeMeta: resourceType}
)

Expand Down