Skip to content

Commit

Permalink
Merge pull request kubevirt#11545 from ormergi/cloudinit-secret-creation
Browse files Browse the repository at this point in the history
tests,cloud-init: Move secret creation to package
  • Loading branch information
kubevirt-bot authored Mar 27, 2024
2 parents b1d9476 + caf61bd commit 7c9813e
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 75 deletions.
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,7 @@ lint:
tests/libnode/... \
tests/libpod/... \
tests/libvmifact/... \
tests/libsecret/... \
&& \
golangci-lint run --disable-all -E ginkgolinter --timeout 10m --verbose --no-config \
./pkg/... \
Expand Down
1 change: 1 addition & 0 deletions tests/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,7 @@ go_test(
"//tests/libnode:go_default_library",
"//tests/libpod:go_default_library",
"//tests/libreplicaset:go_default_library",
"//tests/libsecret:go_default_library",
"//tests/libssh:go_default_library",
"//tests/libstorage:go_default_library",
"//tests/libvmifact:go_default_library",
Expand Down
12 changes: 12 additions & 0 deletions tests/libsecret/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
load("@io_bazel_rules_go//go:def.bzl", "go_library")

go_library(
name = "go_default_library",
srcs = ["secret.go"],
importpath = "kubevirt.io/kubevirt/tests/libsecret",
visibility = ["//visibility:public"],
deps = [
"//vendor/k8s.io/api/core/v1:go_default_library",
"//vendor/k8s.io/apimachinery/pkg/apis/meta/v1:go_default_library",
],
)
40 changes: 40 additions & 0 deletions tests/libsecret/secret.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*
* This file is part of the KubeVirt project
*
* 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.
*
* Copyright The KubeVirt Authors.
*
*/

package libsecret

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

// New return Secret of Opaque type with "kubevirt.io/secret" label
func New(name string, data map[string][]byte) *kubev1.Secret {
// secretLabel set this label to make the test suite namespace clean-up delete the secret on teardown
const secretLabel = "kubevirt.io/secret" // #nosec G101
return &kubev1.Secret{
ObjectMeta: metav1.ObjectMeta{
Name: name,
Labels: map[string]string{
secretLabel: name,
},
},
Data: data,
}
}
99 changes: 24 additions & 75 deletions tests/vmi_cloudinit_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,10 @@ import (
"kubevirt.io/kubevirt/tests/exec"
"kubevirt.io/kubevirt/tests/framework/kubevirt"
"kubevirt.io/kubevirt/tests/libpod"
"kubevirt.io/kubevirt/tests/libsecret"
"kubevirt.io/kubevirt/tests/libvmifact"
"kubevirt.io/kubevirt/tests/libwait"
"kubevirt.io/kubevirt/tests/testsuite"
"kubevirt.io/kubevirt/tests/util"

v1 "kubevirt.io/api/core/v1"
"kubevirt.io/client-go/kubecli"
Expand Down Expand Up @@ -324,20 +324,10 @@ var _ = Describe("[rfe_id:151][crit:high][vendor:[email protected]][level:compon

// Store userdata as k8s secret
By("Creating a user-data secret")
secret := kubev1.Secret{
ObjectMeta: metav1.ObjectMeta{
Name: secretID,
Namespace: vmi.Namespace,
Labels: map[string]string{
util.SecretLabel: secretID,
},
},
Type: "Opaque",
Data: map[string][]byte{
"userdata": []byte(userData), // The client encrypts the secret for us
},
}
_, err := virtClient.CoreV1().Secrets(vmi.Namespace).Create(context.Background(), &secret, metav1.CreateOptions{})
secret := libsecret.New(secretID, map[string][]byte{
"userdata": []byte(userData),
})
_, err := virtClient.CoreV1().Secrets(vmi.Namespace).Create(context.Background(), secret, metav1.CreateOptions{})
Expect(err).ToNot(HaveOccurred())
break
}
Expand Down Expand Up @@ -405,21 +395,10 @@ var _ = Describe("[rfe_id:151][crit:high][vendor:[email protected]][level:compon

// Store cloudinit data as k8s secret
By("Creating a secret with network data")
secret := kubev1.Secret{
ObjectMeta: metav1.ObjectMeta{
Name: secretID,
Namespace: vmi.Namespace,
Labels: map[string]string{
util.SecretLabel: secretID,
},
},
Type: "Opaque",
Data: map[string][]byte{
// The client encrypts the secret for us
"networkdata": []byte(testNetworkData),
},
}
_, err := virtClient.CoreV1().Secrets(vmi.Namespace).Create(context.Background(), &secret, metav1.CreateOptions{})
secret := libsecret.New(secretID, map[string][]byte{
"networkdata": []byte(testNetworkData),
})
_, err := virtClient.CoreV1().Secrets(vmi.Namespace).Create(context.Background(), secret, metav1.CreateOptions{})
Expect(err).ToNot(HaveOccurred())

break
Expand Down Expand Up @@ -548,21 +527,11 @@ var _ = Describe("[rfe_id:151][crit:high][vendor:[email protected]][level:compon

// Store cloudinit data as k8s secret
By("Creating a secret with user and network data")
secret := kubev1.Secret{
ObjectMeta: metav1.ObjectMeta{
Name: secretID,
Namespace: vmi.Namespace,
Labels: map[string]string{
util.SecretLabel: secretID,
},
},
Type: "Opaque",
Data: map[string][]byte{
// The client encrypts the secret for us
"networkdata": []byte(testNetworkData),
},
}
_, err := virtClient.CoreV1().Secrets(vmi.Namespace).Create(context.Background(), &secret, metav1.CreateOptions{})
secret := libsecret.New(secretID, map[string][]byte{
"networkdata": []byte(testNetworkData),
})

_, err := virtClient.CoreV1().Secrets(vmi.Namespace).Create(context.Background(), secret, metav1.CreateOptions{})
Expect(err).ToNot(HaveOccurred())

break
Expand Down Expand Up @@ -606,39 +575,19 @@ var _ = Describe("[rfe_id:151][crit:high][vendor:[email protected]][level:compon

// Store cloudinit data as k8s secret
By("Creating a secret with userdata")
uSecret := kubev1.Secret{
ObjectMeta: metav1.ObjectMeta{
Name: uSecretID,
Namespace: vmi.Namespace,
Labels: map[string]string{
util.SecretLabel: uSecretID,
},
},
Type: "Opaque",
Data: map[string][]byte{
// The client encrypts the secret for us
userDataLabel: []byte(testUserData),
},
}
uSecret := libsecret.New(uSecretID, map[string][]byte{
userDataLabel: []byte(testUserData),
})

By("Creating a secret with network data")
nSecret := kubev1.Secret{
ObjectMeta: metav1.ObjectMeta{
Name: nSecretID,
Namespace: vmi.Namespace,
Labels: map[string]string{
util.SecretLabel: nSecretID,
},
},
Type: "Opaque",
Data: map[string][]byte{
// The client encrypts the secret for us
networkDataLabel: []byte(testNetworkData),
},
}
_, err := virtClient.CoreV1().Secrets(vmi.Namespace).Create(context.Background(), &uSecret, metav1.CreateOptions{})
nSecret := libsecret.New(nSecretID, map[string][]byte{
networkDataLabel: []byte(testNetworkData),
})

_, err := virtClient.CoreV1().Secrets(vmi.Namespace).Create(context.Background(), uSecret, metav1.CreateOptions{})
Expect(err).ToNot(HaveOccurred())

_, err = virtClient.CoreV1().Secrets(vmi.Namespace).Create(context.Background(), &nSecret, metav1.CreateOptions{})
_, err = virtClient.CoreV1().Secrets(vmi.Namespace).Create(context.Background(), nSecret, metav1.CreateOptions{})
Expect(err).ToNot(HaveOccurred())

break
Expand Down

0 comments on commit 7c9813e

Please sign in to comment.