Skip to content
Open
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
82 changes: 82 additions & 0 deletions api/v1/nodenetworkconfigurationenactment_types.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
/*
Copyright The Kubernetes NMState Authors.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package v1

import (
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"

"github.com/nmstate/kubernetes-nmstate/api/names"
"github.com/nmstate/kubernetes-nmstate/api/shared"
)

// +kubebuilder:object:root=true

// NodeNetworkConfigurationEnactmentList contains a list of NodeNetworkConfigurationEnactment
type NodeNetworkConfigurationEnactmentList struct {
metav1.TypeMeta `json:",inline"`
metav1.ListMeta `json:"metadata,omitempty"`
Items []NodeNetworkConfigurationEnactment `json:"items"`
}

// +genclient
// +kubebuilder:object:root=true
// +kubebuilder:subresource:status
// +kubebuilder:resource:path=nodenetworkconfigurationenactments,shortName=nnce,scope=Cluster
// +kubebuilder:printcolumn:name="Status",type="string",JSONPath=".status.conditions[?(@.status==\"True\")].type",description="Status"
//nolint:lll
// +kubebuilder:printcolumn:name="Status Age",type="date",JSONPath=".status.conditions[?(@.status==\"True\")].lastTransitionTime",description="Status Age"
// +kubebuilder:printcolumn:name="Reason",type="string",JSONPath=".status.conditions[?(@.status==\"True\")].reason",description="Reason"
// +kubebuilder:pruning:PreserveUnknownFields
// +kubebuilder:storageversion

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

Status shared.NodeNetworkConfigurationEnactmentStatus `json:"status,omitempty"`
}

func NewEnactment(node *corev1.Node, policy *NodeNetworkConfigurationPolicy) NodeNetworkConfigurationEnactment {
enactment := NodeNetworkConfigurationEnactment{
ObjectMeta: metav1.ObjectMeta{
Name: shared.EnactmentKey(node.Name, policy.Name).Name,
OwnerReferences: []metav1.OwnerReference{
{Name: node.Name, Kind: "Node", APIVersion: "v1", UID: node.UID},
},
// Associate policy and node with the enactment using labels
Labels: names.IncludeRelationshipLabels(map[string]string{
shared.EnactmentPolicyLabel: policy.Name,
shared.EnactmentNodeLabel: node.Name,
}),
},
Status: shared.NodeNetworkConfigurationEnactmentStatus{
DesiredState: shared.NewState(""),
Conditions: shared.ConditionList{},
},
}

for _, conditionType := range shared.NodeNetworkConfigurationEnactmentConditionTypes {
enactment.Status.Conditions.Set(conditionType, corev1.ConditionUnknown, "", "")
}
return enactment
}

func init() {
SchemeBuilder.Register(&NodeNetworkConfigurationEnactment{}, &NodeNetworkConfigurationEnactmentList{})
}
65 changes: 65 additions & 0 deletions api/v1/nodenetworkconfigurationenactment_types_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
/*
Copyright The Kubernetes NMState Authors.


Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package v1

import (
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"

corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"

"github.com/nmstate/kubernetes-nmstate/api/shared"
)

var _ = Describe("NodeNetworkEnactment", func() {
var (
nncp = NodeNetworkConfigurationPolicy{
TypeMeta: metav1.TypeMeta{
APIVersion: "nmstate.io/v1",
Kind: "NodeNetworkConfigurationPolicy",
},
ObjectMeta: metav1.ObjectMeta{
Name: "policy1",
UID: "12345",
},
}
node = corev1.Node{
ObjectMeta: metav1.ObjectMeta{
Name: "node1",
UID: "54321",
},
}
)

Context("NewEnactment", func() {
It("should have the node as the owner reference of the created enactment", func() {
nnce := NewEnactment(&node, &nncp)
desiredOwnerRefs := []metav1.OwnerReference{
{Name: node.Name, Kind: "Node", APIVersion: "v1", UID: node.UID},
}
Expect(nnce.OwnerReferences).To(Equal(desiredOwnerRefs))
})
It("should have labels assocoating to the policy and the node", func() {
nnce := NewEnactment(&node, &nncp)
Expect(nnce.Labels).To(HaveKeyWithValue(shared.EnactmentPolicyLabel, nncp.Name))
Expect(nnce.Labels).To(HaveKeyWithValue(shared.EnactmentNodeLabel, node.Name))
})
})

})
49 changes: 49 additions & 0 deletions api/v1/nodenetworkstate_types.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/*
Copyright The Kubernetes NMState Authors.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package v1

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

"github.com/nmstate/kubernetes-nmstate/api/shared"
)

// +kubebuilder:subresource:status
// +kubebuilder:resource:path=nodenetworkstates,shortName=nns,scope=Cluster
// +kubebuilder:storageversion
// +kubebuilder:object:root=true

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

Status shared.NodeNetworkStateStatus `json:"status,omitempty"`
}

// +kubebuilder:object:root=true

// NodeNetworkStateList contains a list of NodeNetworkState
type NodeNetworkStateList struct {
metav1.TypeMeta `json:",inline"`
metav1.ListMeta `json:"metadata,omitempty"`
Items []NodeNetworkState `json:"items"`
}

func init() {
SchemeBuilder.Register(&NodeNetworkState{}, &NodeNetworkStateList{})
}
107 changes: 107 additions & 0 deletions api/v1/nodenetworkstate_types_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
/*
Copyright The Kubernetes NMState Authors.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package v1

import (
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"

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

yaml "sigs.k8s.io/yaml"

nmstate "github.com/nmstate/kubernetes-nmstate/api/shared"
)

var _ = Describe("NodeNetworkState", func() {
var (
currentState = nmstate.NewState(`
interfaces:
- name: eth1
type: ethernet
state: down`)

nnsManifest = `
apiVersion: nmstate.io/v1
kind: NodeNetworkState
metadata:
name: node01
creationTimestamp: "1970-01-01T00:00:00Z"
status:
currentState:
interfaces:
- name: eth1
type: ethernet
state: down
lastSuccessfulUpdateTime: "1970-01-01T00:00:00Z"

`
nnsStruct = NodeNetworkState{
TypeMeta: metav1.TypeMeta{
APIVersion: "nmstate.io/v1",
Kind: "NodeNetworkState",
},
ObjectMeta: metav1.ObjectMeta{
Name: "node01",
CreationTimestamp: metav1.Unix(0, 0),
},
Status: nmstate.NodeNetworkStateStatus{
CurrentState: currentState,
LastSuccessfulUpdateTime: metav1.Unix(0, 0),
},
}
)

Context("when read NeworkNodeState struct from yaml", func() {

var nodeNetworkStateStruct NodeNetworkState

BeforeEach(func() {
err := yaml.Unmarshal([]byte(nnsManifest), &nodeNetworkStateStruct)
Expect(err).ToNot(HaveOccurred())
})
It("should successfully parse currentState yaml", func() {
Expect(string(nodeNetworkStateStruct.Status.CurrentState.Raw)).To(MatchYAML(string(nnsStruct.Status.CurrentState.Raw)))
})
It("should successfully parse non state attributes", func() {
Expect(nodeNetworkStateStruct.TypeMeta).To(Equal(nnsStruct.TypeMeta))
Expect(nodeNetworkStateStruct.ObjectMeta).To(Equal(nnsStruct.ObjectMeta))
})
})

Context("when reading NodeNetworkState struct from invalid yaml", func() {
It("should return error", func() {
err := yaml.Unmarshal([]byte("invalid yaml"), &NodeNetworkState{})
Expect(err).To(HaveOccurred())
})
})

Context("when write NetworkNodeState struct to yaml", func() {

var nodeNetworkStateManifest []byte
BeforeEach(func() {
var err error
nodeNetworkStateManifest, err = yaml.Marshal(nnsStruct)
Expect(err).ToNot(HaveOccurred())
})

It("should match the NodeNetworkState manifest", func() {
Expect(string(nodeNetworkStateManifest)).To(MatchYAML(nnsManifest))
})
})

})
30 changes: 30 additions & 0 deletions api/v1/v1_suite_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/*
Copyright The Kubernetes NMState Authors.


Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package v1

import (
"testing"

. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
)

func TestUnit(t *testing.T) {
RegisterFailHandler(Fail)
RunSpecs(t, "API Test Suite")
}
Loading