Skip to content

Commit bbcbd14

Browse files
committed
api: unify v1beta1 with v1
Signed-off-by: Mat Kowalski <[email protected]>
1 parent cac05a2 commit bbcbd14

28 files changed

+5082
-48
lines changed
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
/*
2+
Copyright The Kubernetes NMState Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package v1
18+
19+
import (
20+
corev1 "k8s.io/api/core/v1"
21+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
22+
23+
"github.com/nmstate/kubernetes-nmstate/api/names"
24+
"github.com/nmstate/kubernetes-nmstate/api/shared"
25+
)
26+
27+
// +kubebuilder:object:root=true
28+
29+
// NodeNetworkConfigurationEnactmentList contains a list of NodeNetworkConfigurationEnactment
30+
type NodeNetworkConfigurationEnactmentList struct {
31+
metav1.TypeMeta `json:",inline"`
32+
metav1.ListMeta `json:"metadata,omitempty"`
33+
Items []NodeNetworkConfigurationEnactment `json:"items"`
34+
}
35+
36+
// +genclient
37+
// +kubebuilder:object:root=true
38+
// +kubebuilder:subresource:status
39+
// +kubebuilder:resource:path=nodenetworkconfigurationenactments,shortName=nnce,scope=Cluster
40+
// +kubebuilder:printcolumn:name="Status",type="string",JSONPath=".status.conditions[?(@.status==\"True\")].type",description="Status"
41+
//nolint:lll
42+
// +kubebuilder:printcolumn:name="Status Age",type="date",JSONPath=".status.conditions[?(@.status==\"True\")].lastTransitionTime",description="Status Age"
43+
// +kubebuilder:printcolumn:name="Reason",type="string",JSONPath=".status.conditions[?(@.status==\"True\")].reason",description="Reason"
44+
// +kubebuilder:pruning:PreserveUnknownFields
45+
// +kubebuilder:storageversion
46+
47+
// NodeNetworkConfigurationEnactment is the Schema for the nodenetworkconfigurationenactments API
48+
type NodeNetworkConfigurationEnactment struct {
49+
metav1.TypeMeta `json:",inline"`
50+
metav1.ObjectMeta `json:"metadata,omitempty"`
51+
52+
Status shared.NodeNetworkConfigurationEnactmentStatus `json:"status,omitempty"`
53+
}
54+
55+
func NewEnactment(node *corev1.Node, policy *NodeNetworkConfigurationPolicy) NodeNetworkConfigurationEnactment {
56+
enactment := NodeNetworkConfigurationEnactment{
57+
ObjectMeta: metav1.ObjectMeta{
58+
Name: shared.EnactmentKey(node.Name, policy.Name).Name,
59+
OwnerReferences: []metav1.OwnerReference{
60+
{Name: node.Name, Kind: "Node", APIVersion: "v1", UID: node.UID},
61+
},
62+
// Associate policy and node with the enactment using labels
63+
Labels: names.IncludeRelationshipLabels(map[string]string{
64+
shared.EnactmentPolicyLabel: policy.Name,
65+
shared.EnactmentNodeLabel: node.Name,
66+
}),
67+
},
68+
Status: shared.NodeNetworkConfigurationEnactmentStatus{
69+
DesiredState: shared.NewState(""),
70+
Conditions: shared.ConditionList{},
71+
},
72+
}
73+
74+
for _, conditionType := range shared.NodeNetworkConfigurationEnactmentConditionTypes {
75+
enactment.Status.Conditions.Set(conditionType, corev1.ConditionUnknown, "", "")
76+
}
77+
return enactment
78+
}
79+
80+
func init() {
81+
SchemeBuilder.Register(&NodeNetworkConfigurationEnactment{}, &NodeNetworkConfigurationEnactmentList{})
82+
}
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
/*
2+
Copyright The Kubernetes NMState Authors.
3+
4+
5+
Licensed under the Apache License, Version 2.0 (the "License");
6+
you may not use this file except in compliance with the License.
7+
You may obtain a copy of the License at
8+
9+
http://www.apache.org/licenses/LICENSE-2.0
10+
11+
Unless required by applicable law or agreed to in writing, software
12+
distributed under the License is distributed on an "AS IS" BASIS,
13+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
See the License for the specific language governing permissions and
15+
limitations under the License.
16+
*/
17+
18+
package v1
19+
20+
import (
21+
. "github.com/onsi/ginkgo/v2"
22+
. "github.com/onsi/gomega"
23+
24+
corev1 "k8s.io/api/core/v1"
25+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
26+
27+
"github.com/nmstate/kubernetes-nmstate/api/shared"
28+
)
29+
30+
var _ = Describe("NodeNetworkEnactment", func() {
31+
var (
32+
nncp = NodeNetworkConfigurationPolicy{
33+
TypeMeta: metav1.TypeMeta{
34+
APIVersion: "nmstate.io/v1",
35+
Kind: "NodeNetworkConfigurationPolicy",
36+
},
37+
ObjectMeta: metav1.ObjectMeta{
38+
Name: "policy1",
39+
UID: "12345",
40+
},
41+
}
42+
node = corev1.Node{
43+
ObjectMeta: metav1.ObjectMeta{
44+
Name: "node1",
45+
UID: "54321",
46+
},
47+
}
48+
)
49+
50+
Context("NewEnactment", func() {
51+
It("should have the node as the owner reference of the created enactment", func() {
52+
nnce := NewEnactment(&node, &nncp)
53+
desiredOwnerRefs := []metav1.OwnerReference{
54+
{Name: node.Name, Kind: "Node", APIVersion: "v1", UID: node.UID},
55+
}
56+
Expect(nnce.OwnerReferences).To(Equal(desiredOwnerRefs))
57+
})
58+
It("should have labels assocoating to the policy and the node", func() {
59+
nnce := NewEnactment(&node, &nncp)
60+
Expect(nnce.Labels).To(HaveKeyWithValue(shared.EnactmentPolicyLabel, nncp.Name))
61+
Expect(nnce.Labels).To(HaveKeyWithValue(shared.EnactmentNodeLabel, node.Name))
62+
})
63+
})
64+
65+
})

api/v1/nodenetworkstate_types.go

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/*
2+
Copyright The Kubernetes NMState Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package v1
18+
19+
import (
20+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
21+
22+
"github.com/nmstate/kubernetes-nmstate/api/shared"
23+
)
24+
25+
// +kubebuilder:subresource:status
26+
// +kubebuilder:resource:path=nodenetworkstates,shortName=nns,scope=Cluster
27+
// +kubebuilder:storageversion
28+
// +kubebuilder:object:root=true
29+
30+
// NodeNetworkState is the Schema for the nodenetworkstates API
31+
type NodeNetworkState struct {
32+
metav1.TypeMeta `json:",inline"`
33+
metav1.ObjectMeta `json:"metadata,omitempty"`
34+
35+
Status shared.NodeNetworkStateStatus `json:"status,omitempty"`
36+
}
37+
38+
// +kubebuilder:object:root=true
39+
40+
// NodeNetworkStateList contains a list of NodeNetworkState
41+
type NodeNetworkStateList struct {
42+
metav1.TypeMeta `json:",inline"`
43+
metav1.ListMeta `json:"metadata,omitempty"`
44+
Items []NodeNetworkState `json:"items"`
45+
}
46+
47+
func init() {
48+
SchemeBuilder.Register(&NodeNetworkState{}, &NodeNetworkStateList{})
49+
}
Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
/*
2+
Copyright The Kubernetes NMState Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package v1
18+
19+
import (
20+
. "github.com/onsi/ginkgo/v2"
21+
. "github.com/onsi/gomega"
22+
23+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
24+
25+
yaml "sigs.k8s.io/yaml"
26+
27+
nmstate "github.com/nmstate/kubernetes-nmstate/api/shared"
28+
)
29+
30+
var _ = Describe("NodeNetworkState", func() {
31+
var (
32+
currentState = nmstate.NewState(`
33+
interfaces:
34+
- name: eth1
35+
type: ethernet
36+
state: down`)
37+
38+
nnsManifest = `
39+
apiVersion: nmstate.io/v1
40+
kind: NodeNetworkState
41+
metadata:
42+
name: node01
43+
creationTimestamp: "1970-01-01T00:00:00Z"
44+
status:
45+
currentState:
46+
interfaces:
47+
- name: eth1
48+
type: ethernet
49+
state: down
50+
lastSuccessfulUpdateTime: "1970-01-01T00:00:00Z"
51+
52+
`
53+
nnsStruct = NodeNetworkState{
54+
TypeMeta: metav1.TypeMeta{
55+
APIVersion: "nmstate.io/v1",
56+
Kind: "NodeNetworkState",
57+
},
58+
ObjectMeta: metav1.ObjectMeta{
59+
Name: "node01",
60+
CreationTimestamp: metav1.Unix(0, 0),
61+
},
62+
Status: nmstate.NodeNetworkStateStatus{
63+
CurrentState: currentState,
64+
LastSuccessfulUpdateTime: metav1.Unix(0, 0),
65+
},
66+
}
67+
)
68+
69+
Context("when read NeworkNodeState struct from yaml", func() {
70+
71+
var nodeNetworkStateStruct NodeNetworkState
72+
73+
BeforeEach(func() {
74+
err := yaml.Unmarshal([]byte(nnsManifest), &nodeNetworkStateStruct)
75+
Expect(err).ToNot(HaveOccurred())
76+
})
77+
It("should successfully parse currentState yaml", func() {
78+
Expect(string(nodeNetworkStateStruct.Status.CurrentState.Raw)).To(MatchYAML(string(nnsStruct.Status.CurrentState.Raw)))
79+
})
80+
It("should successfully parse non state attributes", func() {
81+
Expect(nodeNetworkStateStruct.TypeMeta).To(Equal(nnsStruct.TypeMeta))
82+
Expect(nodeNetworkStateStruct.ObjectMeta).To(Equal(nnsStruct.ObjectMeta))
83+
})
84+
})
85+
86+
Context("when reading NodeNetworkState struct from invalid yaml", func() {
87+
It("should return error", func() {
88+
err := yaml.Unmarshal([]byte("invalid yaml"), &NodeNetworkState{})
89+
Expect(err).To(HaveOccurred())
90+
})
91+
})
92+
93+
Context("when write NetworkNodeState struct to yaml", func() {
94+
95+
var nodeNetworkStateManifest []byte
96+
BeforeEach(func() {
97+
var err error
98+
nodeNetworkStateManifest, err = yaml.Marshal(nnsStruct)
99+
Expect(err).ToNot(HaveOccurred())
100+
})
101+
102+
It("should match the NodeNetworkState manifest", func() {
103+
Expect(string(nodeNetworkStateManifest)).To(MatchYAML(nnsManifest))
104+
})
105+
})
106+
107+
})

api/v1/v1_suite_test.go

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/*
2+
Copyright The Kubernetes NMState Authors.
3+
4+
5+
Licensed under the Apache License, Version 2.0 (the "License");
6+
you may not use this file except in compliance with the License.
7+
You may obtain a copy of the License at
8+
9+
http://www.apache.org/licenses/LICENSE-2.0
10+
11+
Unless required by applicable law or agreed to in writing, software
12+
distributed under the License is distributed on an "AS IS" BASIS,
13+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
See the License for the specific language governing permissions and
15+
limitations under the License.
16+
*/
17+
18+
package v1
19+
20+
import (
21+
"testing"
22+
23+
. "github.com/onsi/ginkgo/v2"
24+
. "github.com/onsi/gomega"
25+
)
26+
27+
func TestUnit(t *testing.T) {
28+
RegisterFailHandler(Fail)
29+
RunSpecs(t, "API Test Suite")
30+
}

0 commit comments

Comments
 (0)