Skip to content

Commit

Permalink
Return an error when receiving no provider or providerRef
Browse files Browse the repository at this point in the history
  • Loading branch information
jonathan-innis committed Feb 2, 2023
1 parent 9766dd1 commit ce1dafe
Show file tree
Hide file tree
Showing 6 changed files with 18 additions and 16 deletions.
2 changes: 1 addition & 1 deletion hack/toolchain.sh
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ tools() {
go install github.com/mikefarah/yq/v4@v4.24.5
go install github.com/norwoodj/helm-docs/cmd/helm-docs@v1.8.1
go install sigs.k8s.io/controller-runtime/tools/setup-envtest@v0.0.0-20220421205612-c162794a9b12
go install sigs.k8s.io/controller-tools/cmd/controller-gen@v0.8.0
go install sigs.k8s.io/controller-tools/cmd/controller-gen@v0.11.2
go install github.com/sigstore/cosign/cmd/cosign@v1.10.0
go install github.com/gohugoio/hugo@v0.97.3+extended
go install golang.org/x/vuln/cmd/govulncheck@v0.0.0-20220902211423-27dd78d2ca39
Expand Down
8 changes: 1 addition & 7 deletions pkg/apis/crds/karpenter.sh_machines.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ apiVersion: apiextensions.k8s.io/v1
kind: CustomResourceDefinition
metadata:
annotations:
controller-gen.kubebuilder.io/version: v0.8.0
controller-gen.kubebuilder.io/version: v0.11.2
creationTimestamp: null
name: machines.karpenter.sh
spec:
Expand Down Expand Up @@ -327,9 +327,3 @@ spec:
storage: true
subresources:
status: {}
status:
acceptedNames:
kind: ""
plural: ""
conditions: []
storedVersions: []
8 changes: 1 addition & 7 deletions pkg/apis/crds/karpenter.sh_provisioners.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ apiVersion: apiextensions.k8s.io/v1
kind: CustomResourceDefinition
metadata:
annotations:
controller-gen.kubebuilder.io/version: v0.8.0
controller-gen.kubebuilder.io/version: v0.11.2
creationTimestamp: null
name: provisioners.karpenter.sh
spec:
Expand Down Expand Up @@ -370,9 +370,3 @@ spec:
storage: true
subresources:
status: {}
status:
acceptedNames:
kind: ""
plural: ""
conditions: []
storedVersions: []
1 change: 1 addition & 0 deletions pkg/apis/v1alpha5/machine.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ type ProviderRef struct {
// Kind of the referent; More info: https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#types-kinds"
Kind string `json:"kind,omitempty"`
// Name of the referent; More info: http://kubernetes.io/docs/user-guide/identifiers#names
// +required
Name string `json:"name,omitempty"`
// API version of the referent
// +optional
Expand Down
4 changes: 4 additions & 0 deletions pkg/apis/v1alpha5/provisioner_validation.go
Original file line number Diff line number Diff line change
Expand Up @@ -185,10 +185,14 @@ func (s *ProvisionerSpec) validateRequirements() (errs *apis.FieldError) {
return errs
}

// validateProvider checks if exactly one of provider and providerRef are set
func (s *ProvisionerSpec) validateProvider() *apis.FieldError {
if s.Provider != nil && s.ProviderRef != nil {
return apis.ErrMultipleOneOf(providerPath, providerRefPath)
}
if s.Provider == nil && s.ProviderRef == nil {
return apis.ErrMissingOneOf(providerPath, providerRefPath)
}
return nil
}

Expand Down
11 changes: 10 additions & 1 deletion pkg/apis/v1alpha5/suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,12 @@ var _ = Describe("Validation", func() {
BeforeEach(func() {
provisioner = &Provisioner{
ObjectMeta: metav1.ObjectMeta{Name: strings.ToLower(randomdata.SillyName())},
Spec: ProvisionerSpec{},
Spec: ProvisionerSpec{
ProviderRef: &ProviderRef{
Kind: "NodeTemplate",
Name: "default",
},
},
}
})

Expand Down Expand Up @@ -104,6 +109,10 @@ var _ = Describe("Validation", func() {
provisioner.Spec.ProviderRef = &ProviderRef{Name: "providerRef"}
Expect(provisioner.Validate(ctx)).ToNot(Succeed())
})
It("should require at least one of provider and providerRef", func() {
provisioner.Spec.ProviderRef = nil
Expect(provisioner.Validate(ctx)).ToNot(Succeed())
})
})
Context("Labels", func() {
It("should allow unrecognized labels", func() {
Expand Down

0 comments on commit ce1dafe

Please sign in to comment.