Skip to content
Merged
Show file tree
Hide file tree
Changes from 6 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
28 changes: 28 additions & 0 deletions apis/networking/network_type.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,41 @@ package networking

import metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"

// NetworkSpec defines the desired state of Network
type NetworkSpec struct {
// ProviderID is the identifier of the network provider.
ProviderID string
}

// NetworkStatus defines the observed state of Network
type NetworkStatus struct {
// State is the state of the machine.
State NetworkState
}

// NetworkState is the state of a network.
// +enum
type NetworkState string

const (
// NetworkStatePending means the network is being provisioned.
NetworkStatePending NetworkState = "Pending"
// NetworkStateAvailable means the network is ready to use.
NetworkStateAvailable NetworkState = "Available"
// NetworkStateError means the network is in an error state.
NetworkStateError NetworkState = "Error"
)

// +genclient
// +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object

// Network is the Schema for the network API
type Network struct {
metav1.TypeMeta
metav1.ObjectMeta

Spec NetworkSpec
Status NetworkStatus
}

// +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object
Expand Down
28 changes: 28 additions & 0 deletions apis/networking/v1alpha1/network_type.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,41 @@ package v1alpha1

import metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"

// NetworkSpec defines the desired state of Network
type NetworkSpec struct {
// ProviderID is the identifier of the network provider.
ProviderID string `json:"providerID"`
}

// NetworkStatus defines the observed state of Network
type NetworkStatus struct {
// State is the state of the machine.
State NetworkState `json:"state,omitempty"`
}

// NetworkState is the state of a network.
// +enum
type NetworkState string

const (
// NetworkStatePending means the network is being provisioned.
NetworkStatePending NetworkState = "Pending"
// NetworkStateAvailable means the network is ready to use.
NetworkStateAvailable NetworkState = "Available"
// NetworkStateError means the network is in an error state.
NetworkStateError NetworkState = "Error"
)

// +genclient
// +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object

// Network is the Schema for the network API
type Network struct {
metav1.TypeMeta `json:",inline"`
metav1.ObjectMeta `json:"metadata,omitempty"`

Spec NetworkSpec `json:"spec,omitempty"`
Status NetworkStatus `json:"status,omitempty"`
}

// +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object
Expand Down
72 changes: 72 additions & 0 deletions apis/networking/v1alpha1/zz_generated.conversion.go

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

34 changes: 34 additions & 0 deletions apis/networking/v1alpha1/zz_generated.deepcopy.go

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

19 changes: 19 additions & 0 deletions apis/networking/validation/network.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package validation

import (
"github.com/onmetal/onmetal-api/apis/networking"
onmetalapivalidation "github.com/onmetal/onmetal-api/onmetal-apiserver/api/validation"
apivalidation "k8s.io/apimachinery/pkg/api/validation"
"k8s.io/apimachinery/pkg/util/validation/field"
)
Expand All @@ -27,6 +28,13 @@ func ValidateNetwork(network *networking.Network) field.ErrorList {
var allErrs field.ErrorList

allErrs = append(allErrs, apivalidation.ValidateObjectMetaAccessor(network, true, apivalidation.NameIsDNSLabel, field.NewPath("metadata"))...)
allErrs = append(allErrs, validateNetworkSpec(&network.Spec, field.NewPath("spec"))...)

return allErrs
}

func validateNetworkSpec(networkSpec *networking.NetworkSpec, fldPath *field.Path) field.ErrorList {
var allErrs field.ErrorList

return allErrs
}
Expand All @@ -36,7 +44,18 @@ func ValidateNetworkUpdate(newNetwork, oldNetwork *networking.Network) field.Err
var allErrs field.ErrorList

allErrs = append(allErrs, apivalidation.ValidateObjectMetaAccessorUpdate(newNetwork, oldNetwork, field.NewPath("metadata"))...)
allErrs = append(allErrs, validateNetworkSpecUpdate(&newNetwork.Spec, &oldNetwork.Spec, field.NewPath("spec"))...)
allErrs = append(allErrs, ValidateNetwork(newNetwork)...)

return allErrs
}

func validateNetworkSpecUpdate(newSpec, oldSpec *networking.NetworkSpec, fldPath *field.Path) field.ErrorList {
var allErrs field.ErrorList

if oldSpec.ProviderID != "" {
allErrs = append(allErrs, onmetalapivalidation.ValidateImmutableField(newSpec.ProviderID, oldSpec.ProviderID, fldPath.Child("providerID"))...)
}

return allErrs
}
29 changes: 29 additions & 0 deletions apis/networking/validation/network_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,33 @@ var _ = Describe("Network", func() {
ContainElement(InvalidField("metadata.name")),
),
)

DescribeTable("ValidateNetworkUpdate",
func(newNetwork, oldNetwork *networking.Network, match types.GomegaMatcher) {
errList := ValidateNetworkUpdate(newNetwork, oldNetwork)
Expect(errList).To(match)
},
Entry("immutable providerID if set",
&networking.Network{
Spec: networking.NetworkSpec{
ProviderID: "foo",
},
},
&networking.Network{
Spec: networking.NetworkSpec{
ProviderID: "bar",
},
},
ContainElement(ImmutableField("spec.providerID")),
),
Entry("mutable providerID if not set",
&networking.Network{
Spec: networking.NetworkSpec{
ProviderID: "foo",
},
},
&networking.Network{},
Not(ContainElement(ImmutableField("spec.providerID"))),
),
)
})
34 changes: 34 additions & 0 deletions apis/networking/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 config/samples/networking_v1alpha1_network.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@ apiVersion: networking.api.onmetal.de/v1alpha1
kind: Network
metadata:
namespace: default
name: network-sample
name: network-sample

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

Loading