Skip to content

Commit 7844cbc

Browse files
authored
nad: switch package to runtime added unit tests (rh-ecosystem-edge#621)
Co-authored-by: nkononov <[email protected]>
1 parent 2cfd6b2 commit 7844cbc

File tree

4 files changed

+519
-24
lines changed

4 files changed

+519
-24
lines changed

pkg/clients/clients.go

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,6 @@ import (
2323
"k8s.io/client-go/tools/clientcmd"
2424
runtimeClient "sigs.k8s.io/controller-runtime/pkg/client"
2525

26-
netAttDefV1 "github.com/k8snetworkplumbingwg/network-attachment-definition-client/pkg/apis/k8s.cni.cncf.io/v1"
27-
clientNetAttDefV1 "github.com/k8snetworkplumbingwg/network-attachment-definition-client/pkg/client/clientset/versioned/typed/k8s.cni.cncf.io/v1"
28-
2926
clientMachineConfigFake "github.com/openshift/machine-config-operator/pkg/generated/clientset/versioned/fake"
3027
clientMachineConfigV1 "github.com/openshift/machine-config-operator/pkg/generated/clientset/versioned/typed/machineconfiguration.openshift.io/v1"
3128

@@ -73,7 +70,6 @@ type Settings struct {
7370
Config *rest.Config
7471
runtimeClient.Client
7572
v1security.SecurityV1Interface
76-
clientNetAttDefV1.K8sCniCncfIoV1Interface
7773
dynamic.Interface
7874
MultiNetworkPolicyClient multinetpolicyclientv1.K8sCniCncfIoV1beta1Interface
7975
multinetpolicyclientv1.K8sCniCncfIoV1beta1Interface
@@ -118,7 +114,6 @@ func New(kubeconfig string) *Settings {
118114
clientSet.AppsV1Interface = appsV1Client.NewForConfigOrDie(config)
119115
clientSet.NetworkingV1Interface = networkV1Client.NewForConfigOrDie(config)
120116
clientSet.RbacV1Interface = rbacV1Client.NewForConfigOrDie(config)
121-
clientSet.K8sCniCncfIoV1Interface = clientNetAttDefV1.NewForConfigOrDie(config)
122117
clientSet.Interface = dynamic.NewForConfigOrDie(config)
123118
clientSet.SecurityV1Interface = v1security.NewForConfigOrDie(config)
124119
clientSet.OperatorV1alpha1Interface = operatorv1alpha1.NewForConfigOrDie(config)
@@ -158,10 +153,6 @@ func SetScheme(crScheme *runtime.Scheme) error {
158153
return err
159154
}
160155

161-
if err := netAttDefV1.SchemeBuilder.AddToScheme(crScheme); err != nil {
162-
return err
163-
}
164-
165156
if err := mcv1.AddToScheme(crScheme); err != nil {
166157
return err
167158
}

pkg/nad/builder.go

Lines changed: 81 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
k8serrors "k8s.io/apimachinery/pkg/api/errors"
99
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
1010
"k8s.io/apimachinery/pkg/runtime/schema"
11+
runtimeClient "sigs.k8s.io/controller-runtime/pkg/client"
1112

1213
"context"
1314
"encoding/json"
@@ -19,7 +20,7 @@ type Builder struct {
1920
Definition *nadV1.NetworkAttachmentDefinition
2021
Object *nadV1.NetworkAttachmentDefinition
2122
metaPluginConfigs []Plugin
22-
apiClient *clients.Settings
23+
apiClient runtimeClient.Client
2324
errorMsg string
2425
}
2526

@@ -36,6 +37,19 @@ func NewBuilder(apiClient *clients.Settings, name, nsname string) *Builder {
3637
"name: %s, namespace: %s",
3738
name, nsname)
3839

40+
if apiClient == nil {
41+
glog.V(100).Infof("The apiClient cannot be nil")
42+
43+
return nil
44+
}
45+
46+
err := apiClient.AttachScheme(nadV1.AddToScheme)
47+
if err != nil {
48+
glog.V(100).Infof("Failed to add nad v1 scheme to client schemes")
49+
50+
return nil
51+
}
52+
3953
builder := Builder{
4054
apiClient: apiClient,
4155
Definition: &nadV1.NetworkAttachmentDefinition{
@@ -63,7 +77,21 @@ func NewBuilder(apiClient *clients.Settings, name, nsname string) *Builder {
6377

6478
// Pull pulls existing networkattachmentdefinition from cluster.
6579
func Pull(apiClient *clients.Settings, name, nsname string) (*Builder, error) {
66-
glog.V(100).Infof("Pulling existing networkattachmentdefinition name %s under namespace %s from cluster", name, nsname)
80+
glog.V(100).Infof(
81+
"Pulling existing networkattachmentdefinition name %s under namespace %s from cluster", name, nsname)
82+
83+
if apiClient == nil {
84+
glog.V(100).Infof("The apiClient cannot be nil")
85+
86+
return nil, fmt.Errorf("the apiClient cannot be nil")
87+
}
88+
89+
err := apiClient.AttachScheme(nadV1.AddToScheme)
90+
if err != nil {
91+
glog.V(100).Infof("Failed to add nad v1 scheme to client schemes")
92+
93+
return nil, fmt.Errorf("failed to add nad v1 scheme to client schemes")
94+
}
6795

6896
builder := Builder{
6997
apiClient: apiClient,
@@ -78,13 +106,13 @@ func Pull(apiClient *clients.Settings, name, nsname string) (*Builder, error) {
78106
if name == "" {
79107
glog.V(100).Infof("The name of the networkattachmentdefinition is empty")
80108

81-
builder.errorMsg = "networkattachmentdefinition 'name' cannot be empty"
109+
return nil, fmt.Errorf("networkattachmentdefinition 'name' cannot be empty")
82110
}
83111

84112
if nsname == "" {
85113
glog.V(100).Infof("The namespace of the networkattachmentdefinition is empty")
86114

87-
builder.errorMsg = "networkattachmentdefinition 'namespace' cannot be empty"
115+
return nil, fmt.Errorf("networkattachmentdefinition 'namespace' cannot be empty")
88116
}
89117

90118
if !builder.Exists() {
@@ -96,6 +124,32 @@ func Pull(apiClient *clients.Settings, name, nsname string) (*Builder, error) {
96124
return &builder, nil
97125
}
98126

127+
// Get returns CatalogSource object if found.
128+
func (builder *Builder) Get() (*nadV1.NetworkAttachmentDefinition, error) {
129+
if valid, err := builder.validate(); !valid {
130+
return nil, err
131+
}
132+
133+
glog.V(100).Infof(
134+
"Collecting NetworkAttachmentDefinition object %s in namespace %s",
135+
builder.Definition.Name, builder.Definition.Namespace)
136+
137+
network := &nadV1.NetworkAttachmentDefinition{}
138+
err := builder.apiClient.Get(context.TODO(),
139+
runtimeClient.ObjectKey{Name: builder.Definition.Name, Namespace: builder.Definition.Namespace},
140+
network)
141+
142+
if err != nil {
143+
glog.V(100).Infof(
144+
"NetworkAttachmentDefinition object %s does not exist in namespace %s",
145+
builder.Definition.Name, builder.Definition.Namespace)
146+
147+
return nil, err
148+
}
149+
150+
return network, nil
151+
}
152+
99153
// Create builds a NetworkAttachmentDefinition resource with the builder configuration.
100154
//
101155
// if the creation failed, the builder errorMsg will be updated.
@@ -118,13 +172,17 @@ func (builder *Builder) Create() (*Builder, error) {
118172
}
119173

120174
if !builder.Exists() {
121-
builder.Object, err = builder.apiClient.NetworkAttachmentDefinitions(builder.Definition.Namespace).
122-
Create(context.TODO(), builder.Definition, metav1.CreateOptions{})
175+
err := builder.apiClient.Create(context.TODO(), builder.Definition)
176+
123177
if err != nil {
124-
return builder, fmt.Errorf("fail to create NAD object due to: " + err.Error())
178+
glog.V(100).Infof("Failed to create NAD object")
179+
180+
return nil, err
125181
}
126182
}
127183

184+
builder.Object = builder.Definition
185+
128186
return builder, nil
129187
}
130188

@@ -140,11 +198,12 @@ func (builder *Builder) Delete() error {
140198
builder.Definition.Name, builder.Definition.Namespace)
141199

142200
if !builder.Exists() {
201+
glog.V(100).Infof("NetworkAttachmentDefinition cannot be deleted because it does not exist")
202+
143203
return nil
144204
}
145205

146-
err := builder.apiClient.NetworkAttachmentDefinitions(builder.Definition.Namespace).Delete(
147-
context.TODO(), builder.Definition.Namespace, metav1.DeleteOptions{})
206+
err := builder.apiClient.Delete(context.TODO(), builder.Definition)
148207

149208
if err != nil {
150209
return fmt.Errorf("fail to delete NAD object due to: %w", err)
@@ -164,13 +223,17 @@ func (builder *Builder) Update() (*Builder, error) {
164223
glog.V(100).Infof("Updating NetworkAttachmentDefinition %s in namespace %s",
165224
builder.Definition.Name, builder.Definition.Namespace)
166225

167-
var err error
226+
if !builder.Exists() {
227+
return nil, fmt.Errorf("failed to update NetworkAttachmentDefinition, object does not exist on cluster")
228+
}
168229

169230
builder.Definition.CreationTimestamp = metav1.Time{}
170231
builder.Definition.ResourceVersion = builder.Object.ResourceVersion
232+
err := builder.apiClient.Update(context.TODO(), builder.Definition)
171233

172-
builder.Object, err = builder.apiClient.NetworkAttachmentDefinitions(builder.Definition.Namespace).Update(
173-
context.TODO(), builder.Definition, metav1.UpdateOptions{})
234+
if err == nil {
235+
builder.Object = builder.Definition
236+
}
174237

175238
return builder, err
176239
}
@@ -187,8 +250,8 @@ func (builder *Builder) Exists() bool {
187250
glog.V(100).Infof("Checking if NetworkAttachmentDefinition %s exists in namespace %s",
188251
builder.Definition.Name, builder.Definition.Namespace)
189252

190-
_, err := builder.apiClient.NetworkAttachmentDefinitions(builder.Definition.Namespace).Get(context.TODO(),
191-
builder.Definition.Name, metav1.GetOptions{})
253+
var err error
254+
builder.Object, err = builder.Get()
192255

193256
return nil == err || !k8serrors.IsNotFound(err)
194257
}
@@ -249,6 +312,10 @@ func (builder *Builder) WithMasterPlugin(masterPlugin *MasterPlugin) *Builder {
249312
return builder
250313
}
251314

315+
if masterPlugin == nil {
316+
builder.errorMsg = "error 'masterPlugin' is empty"
317+
}
318+
252319
glog.V(100).Infof("Adding masterPlugin %v to NAD %s", masterPlugin, builder.Definition.Name)
253320

254321
emptyNadConfig := nadV1.NetworkAttachmentDefinitionSpec{}

0 commit comments

Comments
 (0)