Skip to content

Commit

Permalink
Merge pull request kubevirt#10309 from lyarwood/virt-operator-common-…
Browse files Browse the repository at this point in the history
…instancetypes

virt-operator: Deploy common-instancetypes
  • Loading branch information
kubevirt-bot authored Oct 17, 2023
2 parents e41a085 + f4a8cb2 commit 2823187
Show file tree
Hide file tree
Showing 20 changed files with 3,125 additions and 10 deletions.
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ generate:
hack/dockerized "DOCKER_PREFIX=${DOCKER_PREFIX} DOCKER_TAG=${DOCKER_TAG} IMAGE_PULL_POLICY=${IMAGE_PULL_POLICY} VERBOSITY=${VERBOSITY} ./hack/generate.sh"
SYNC_VENDOR=true hack/dockerized "./hack/bazel-generate.sh && hack/bazel-fmt.sh"
hack/dockerized hack/sync-kubevirtci.sh
hack/dockerized hack/sync-common-instancetypes.sh

generate-verify: generate
./hack/verify-generate.sh
Expand Down
32 changes: 32 additions & 0 deletions hack/bump-common-instancetypes.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#!/bin/bash

set -ex

source $(dirname "$0")/common.sh
source $(dirname "$0")/config.sh

function latest_version() {
local org="$1"
local repo="$2"

curl --fail -s "https://api.github.com/repos/${org}/${repo}/releases/latest" |
jq -r '.tag_name'
}

function checksum() {
local version="$1"
local file="$2"

curl -L "https://github.com/kubevirt/common-instancetypes/releases/download/${version}/CHECKSUMS.sha256" |
grep "${file}" | cut -d " " -f 1
}

version=$(latest_version "kubevirt" "common-instancetypes")
instancetypes_checksum=$(checksum "${version}" "common-clusterinstancetypes-bundle-${version}.yaml")
preferences_checksum=$(checksum "${version}" "common-clusterpreferences-bundle-${version}.yaml")

sed -i "/^[[:blank:]]*common_instancetypes_version[[:blank:]]*=/s/=.*/=\${COMMON_INSTANCETYPES_VERSION:-\"${version}\"}/" hack/config-default.sh
sed -i "/^[[:blank:]]*cluster_instancetypes_sha256[[:blank:]]*=/s/=.*/=\${CLUSTER_INSTANCETYPES_SHA256:-\"${instancetypes_checksum}\"}/" hack/config-default.sh
sed -i "/^[[:blank:]]*cluster_preferences_sha256[[:blank:]]*=/s/=.*/=\${CLUSTER_PREFERENCES_SHA256:-\"${preferences_checksum}\"}/" hack/config-default.sh

hack/sync-common-instancetypes.sh
3 changes: 3 additions & 0 deletions hack/config-default.sh
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@ conn_check_ipv6_address=${CONN_CHECK_IPV6_ADDRESS:-""}
conn_check_dns=${CONN_CHECK_DNS:-""}
migration_network_nic=${MIGRATION_NETWORK_NIC:-"eth1"}
infra_replicas=${KUBEVIRT_INFRA_REPLICAS:-0}
common_instancetypes_version=${COMMON_INSTANCETYPES_VERSION:-"v0.3.4"}
cluster_instancetypes_sha256=${CLUSTER_INSTANCETYPES_SHA256:-"80b6ba927a16e2bff8933ff6b0968158372b21633d38dc99573592c4a136073f"}
cluster_preferences_sha256=${CLUSTER_PREFERENCES_SHA256:-"f5bcffc1a94027564b2e69da19e3e4db3532446209ff05c7e5ffd90894bb3e31"}

# try to derive csv_version from docker tag. But it must start with x.y.z, without leading v
default_csv_version="${docker_tag/latest/0.0.0}"
Expand Down
20 changes: 20 additions & 0 deletions hack/sync-common-instancetypes.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#!/bin/bash

set -ex

source $(dirname "$0")/common.sh
source $(dirname "$0")/config.sh

_common_instancetypes_base_url="https://github.com/kubevirt/common-instancetypes/releases/download"
_cluster_instancetypes_path="pkg/virt-operator/resource/generate/components/data/common-clusterinstancetypes-bundle.yaml"
_cluster_preferences_path="pkg/virt-operator/resource/generate/components/data/common-clusterpreferences-bundle.yaml"

curl \
-L "${_common_instancetypes_base_url}/${common_instancetypes_version}/common-clusterinstancetypes-bundle-${common_instancetypes_version}.yaml" \
-o "${_cluster_instancetypes_path}"
echo "${cluster_instancetypes_sha256} ${_cluster_instancetypes_path}" | sha256sum --check --strict

curl \
-L "${_common_instancetypes_base_url}/${common_instancetypes_version}/common-clusterpreferences-bundle-${common_instancetypes_version}.yaml" \
-o "${_cluster_preferences_path}"
echo "${cluster_preferences_sha256} ${_cluster_preferences_path}" | sha256sum --check --strict
10 changes: 10 additions & 0 deletions pkg/virt-config/feature-gates.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,12 @@ const (
NetworkBindingPlugingsGate = "NetworkBindingPlugins"
// AutoResourceLimitsGate enables automatic setting of vmi limits if there is a ResourceQuota with limits associated with the vmi namespace.
AutoResourceLimitsGate = "AutoResourceLimitsGate"

// Owner: @lyarwood
// Alpha: v1.1.0
//
// CommonInstancetypesDeploymentGate enables the deployment of common-instancetypes by virt-operator
CommonInstancetypesDeploymentGate = "CommonInstancetypesDeploymentGate"
)

var deprecatedFeatureGates = [...]string{
Expand Down Expand Up @@ -251,3 +257,7 @@ func (config *ClusterConfig) NetworkBindingPlugingsEnabled() bool {
func (config *ClusterConfig) AutoResourceLimitsEnabled() bool {
return config.isFeatureGateEnabled(AutoResourceLimitsGate)
}

func (config *ClusterConfig) CommonInstancetypesDeploymentEnabled() bool {
return config.isFeatureGateEnabled(CommonInstancetypesDeploymentGate)
}
4 changes: 4 additions & 0 deletions pkg/virt-operator/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,9 @@ go_test(
"//pkg/virt-operator/resource/generate/rbac:go_default_library",
"//pkg/virt-operator/util:go_default_library",
"//staging/src/kubevirt.io/api/core/v1:go_default_library",
"//staging/src/kubevirt.io/api/instancetype:go_default_library",
"//staging/src/kubevirt.io/api/instancetype/v1beta1:go_default_library",
"//staging/src/kubevirt.io/client-go/generated/kubevirt/clientset/versioned/fake:go_default_library",
"//staging/src/kubevirt.io/client-go/generated/prometheus-operator/clientset/versioned/fake:go_default_library",
"//staging/src/kubevirt.io/client-go/kubecli:go_default_library",
"//staging/src/kubevirt.io/client-go/testutils:go_default_library",
Expand All @@ -100,6 +103,7 @@ go_test(
"//vendor/k8s.io/apiextensions-apiserver/pkg/client/clientset/clientset/fake:go_default_library",
"//vendor/k8s.io/apimachinery/pkg/api/errors:go_default_library",
"//vendor/k8s.io/apimachinery/pkg/apis/meta/v1:go_default_library",
"//vendor/k8s.io/apimachinery/pkg/labels:go_default_library",
"//vendor/k8s.io/apimachinery/pkg/runtime:go_default_library",
"//vendor/k8s.io/apimachinery/pkg/runtime/schema:go_default_library",
"//vendor/k8s.io/apimachinery/pkg/types:go_default_library",
Expand Down
61 changes: 53 additions & 8 deletions pkg/virt-operator/kubevirt_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ import (
extclientfake "k8s.io/apiextensions-apiserver/pkg/client/clientset/clientset/fake"
"k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/apimachinery/pkg/types"
Expand All @@ -63,6 +64,9 @@ import (
"k8s.io/utils/pointer"

v1 "kubevirt.io/api/core/v1"
apiinstancetype "kubevirt.io/api/instancetype"
"kubevirt.io/api/instancetype/v1beta1"
kubevirtfake "kubevirt.io/client-go/generated/kubevirt/clientset/versioned/fake"
promclientfake "kubevirt.io/client-go/generated/prometheus-operator/clientset/versioned/fake"
"kubevirt.io/client-go/kubecli"
"kubevirt.io/client-go/version"
Expand Down Expand Up @@ -126,13 +130,14 @@ type KubeVirtTestData struct {

recorder *record.FakeRecorder

mockQueue *testutils.MockWorkQueue
virtClient *kubecli.MockKubevirtClient
kubeClient *fake.Clientset
secClient *secv1fake.FakeSecurityV1
extClient *extclientfake.Clientset
promClient *promclientfake.Clientset
routeClient *routev1fake.FakeRouteV1
mockQueue *testutils.MockWorkQueue
virtClient *kubecli.MockKubevirtClient
virtFakeClient *kubevirtfake.Clientset
kubeClient *fake.Clientset
secClient *secv1fake.FakeSecurityV1
extClient *extclientfake.Clientset
promClient *promclientfake.Clientset
routeClient *routev1fake.FakeRouteV1

informers util.Informers
stores util.Stores
Expand Down Expand Up @@ -279,6 +284,8 @@ func (k *KubeVirtTestData) BeforeTest() {
Fake: &fake.NewSimpleClientset().Fake,
}

k.virtFakeClient = kubevirtfake.NewSimpleClientset()

k.virtClient.EXPECT().AdmissionregistrationV1().Return(k.kubeClient.AdmissionregistrationV1()).AnyTimes()
k.virtClient.EXPECT().CoreV1().Return(k.kubeClient.CoreV1()).AnyTimes()
k.virtClient.EXPECT().BatchV1().Return(k.kubeClient.BatchV1()).AnyTimes()
Expand All @@ -289,6 +296,8 @@ func (k *KubeVirtTestData) BeforeTest() {
k.virtClient.EXPECT().PolicyV1().Return(k.kubeClient.PolicyV1()).AnyTimes()
k.virtClient.EXPECT().PrometheusClient().Return(k.promClient).AnyTimes()
k.virtClient.EXPECT().RouteClient().Return(k.routeClient).AnyTimes()
k.virtClient.EXPECT().VirtualMachineClusterInstancetype().Return(k.virtFakeClient.InstancetypeV1beta1().VirtualMachineClusterInstancetypes()).AnyTimes()
k.virtClient.EXPECT().VirtualMachineClusterPreference().Return(k.virtFakeClient.InstancetypeV1beta1().VirtualMachineClusterPreferences()).AnyTimes()

// Make sure that all unexpected calls to kubeClient will fail
k.kubeClient.Fake.PrependReactor("*", "*", func(action testing.Action) (handled bool, obj runtime.Object, err error) {
Expand Down Expand Up @@ -334,6 +343,17 @@ func (k *KubeVirtTestData) BeforeTest() {
Expect(action).To(BeNil())
return true, nil, nil
})
k.virtFakeClient.Fake.PrependReactor("*", "*", func(action testing.Action) (handled bool, obj runtime.Object, err error) {
if action.GetVerb() == "list" && action.GetResource().Resource == apiinstancetype.ClusterPluralResourceName {
return true, &v1beta1.VirtualMachineClusterInstancetypeList{}, nil
}
if action.GetVerb() == "list" && action.GetResource().Resource == apiinstancetype.ClusterPluralPreferenceResourceName {
return true, &v1beta1.VirtualMachineClusterPreferenceList{}, nil
}
// Make sure other unexpected calls fail
Expect(action).To(BeNil())
return true, nil, nil
})

syncCaches(k.stop, k.kvInformer, k.informers)

Expand Down Expand Up @@ -1519,7 +1539,8 @@ func (k *KubeVirtTestData) addValidatingWebhook(wh *admissionregistrationv1.Vali

func (k *KubeVirtTestData) addInstallStrategy(config *util.KubeVirtDeploymentConfig) {
// install strategy config
resource, _ := install.NewInstallStrategyConfigMap(config, "openshift-monitoring", NAMESPACE)
resource, err := install.NewInstallStrategyConfigMap(config, "openshift-monitoring", NAMESPACE)
Expect(err).ToNot(HaveOccurred())

resource.Name = fmt.Sprintf("%s-%s", resource.Name, rand.String(10))

Expand Down Expand Up @@ -1669,6 +1690,21 @@ func (k *KubeVirtTestData) getConfig(registry, version string) *util.KubeVirtDep
k.mockEnvVarManager)
}

func (k *KubeVirtTestData) shouldExpectInstancetypesAndPreferencesDeletions() {
expectDeleteCollection := func(action testing.Action) (bool, runtime.Object, error) {
deleteCollection, ok := action.(testing.DeleteCollectionAction)
Expect(ok).To(BeTrue())
ls := labels.Set{
v1.AppComponentLabel: v1.AppComponent,
v1.ManagedByLabel: v1.ManagedByLabelOperatorValue,
}
Expect(deleteCollection.GetListRestrictions().Labels).To(Equal(ls.AsSelector()))
return true, nil, nil
}
k.virtFakeClient.Fake.PrependReactor("delete-collection", apiinstancetype.ClusterPluralResourceName, expectDeleteCollection)
k.virtFakeClient.Fake.PrependReactor("delete-collection", apiinstancetype.ClusterPluralPreferenceResourceName, expectDeleteCollection)
}

var _ = Describe("KubeVirt Operator", func() {

Context("On valid KubeVirt object", func() {
Expand Down Expand Up @@ -1715,6 +1751,7 @@ var _ = Describe("KubeVirt Operator", func() {
kvTestData.makeDeploymentsReady(kv)
kvTestData.makeHandlerReady()
kvTestData.shouldExpectPatchesAndUpdates(kv)
kvTestData.shouldExpectInstancetypesAndPreferencesDeletions()

// Now when the controller runs, if the namespace will be patched, the test will fail
// because the patch is not expected here.
Expand Down Expand Up @@ -1782,6 +1819,7 @@ var _ = Describe("KubeVirt Operator", func() {
kvTestData.fakeNamespaceModificationEvent()
kvTestData.shouldExpectNamespacePatch()
kvTestData.shouldExpectPatchesAndUpdates(kv)
kvTestData.shouldExpectInstancetypesAndPreferencesDeletions()
kvTestData.addAll(customConfig, kv)
// install strategy config
kvTestData.addInstallStrategy(customConfig)
Expand Down Expand Up @@ -1835,6 +1873,7 @@ var _ = Describe("KubeVirt Operator", func() {
kvTestData.shouldExpectNamespacePatch()
kvTestData.shouldExpectPatchesAndUpdates(kv)
kvTestData.shouldExpectKubeVirtUpdateStatus(1)
kvTestData.shouldExpectInstancetypesAndPreferencesDeletions()

kvTestData.controller.Execute()
Expect(kvTestData.totalDeletions).To(Equal(1))
Expand Down Expand Up @@ -1907,6 +1946,7 @@ var _ = Describe("KubeVirt Operator", func() {
kvTestData.shouldExpectNamespacePatch()
kvTestData.shouldExpectPatchesAndUpdates(kv)
kvTestData.shouldExpectKubeVirtUpdateStatus(1)
kvTestData.shouldExpectInstancetypesAndPreferencesDeletions()

kvTestData.controller.Execute()
Expect(kvTestData.totalDeletions).To(Equal(1))
Expand Down Expand Up @@ -1948,6 +1988,7 @@ var _ = Describe("KubeVirt Operator", func() {
kvTestData.shouldExpectNamespacePatch()
kvTestData.shouldExpectPatchesAndUpdates(kv)
kvTestData.shouldExpectKubeVirtUpdateStatus(1)
kvTestData.shouldExpectInstancetypesAndPreferencesDeletions()

kvTestData.controller.Execute()

Expand Down Expand Up @@ -1994,6 +2035,7 @@ var _ = Describe("KubeVirt Operator", func() {
kvTestData.shouldExpectNamespacePatch()
kvTestData.shouldExpectPatchesAndUpdates(kv)
kvTestData.shouldExpectKubeVirtUpdateStatus(1)
kvTestData.shouldExpectInstancetypesAndPreferencesDeletions()

// invalidate all lastGeneration versions
numGenerations := len(kv.Status.Generations)
Expand Down Expand Up @@ -2056,6 +2098,7 @@ var _ = Describe("KubeVirt Operator", func() {
kvTestData.shouldExpectNamespacePatch()
kvTestData.shouldExpectPatchesAndUpdates(kv)
kvTestData.shouldExpectKubeVirtUpdateStatus(1)
kvTestData.shouldExpectInstancetypesAndPreferencesDeletions()

kvTestData.controller.Execute()
Expect(kvTestData.totalDeletions).To(Equal(numResources))
Expand Down Expand Up @@ -2704,6 +2747,7 @@ var _ = Describe("KubeVirt Operator", func() {
kvTestData.shouldExpectKubeVirtUpdateStatus(1)
kvTestData.fakeNamespaceModificationEvent()
kvTestData.shouldExpectNamespacePatch()
kvTestData.shouldExpectInstancetypesAndPreferencesDeletions()

kvTestData.controller.Execute()

Expand Down Expand Up @@ -2781,6 +2825,7 @@ var _ = Describe("KubeVirt Operator", func() {
kvTestData.shouldExpectKubeVirtUpdateStatus(1)
kvTestData.fakeNamespaceModificationEvent()
kvTestData.shouldExpectNamespacePatch()
kvTestData.shouldExpectInstancetypesAndPreferencesDeletions()

kvTestData.controller.Execute()

Expand Down
8 changes: 8 additions & 0 deletions pkg/virt-operator/resource/apply/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ go_library(
"crds.go",
"delete.go",
"generations.go",
"instancetypes.go",
"patches.go",
"prometheus.go",
"rbac.go",
Expand All @@ -32,6 +33,7 @@ go_library(
"//pkg/virt-operator/resource/generate/rbac:go_default_library",
"//pkg/virt-operator/util:go_default_library",
"//staging/src/kubevirt.io/api/core/v1:go_default_library",
"//staging/src/kubevirt.io/api/instancetype/v1beta1:go_default_library",
"//staging/src/kubevirt.io/client-go/kubecli:go_default_library",
"//staging/src/kubevirt.io/client-go/log:go_default_library",
"//vendor/github.com/blang/semver:go_default_library",
Expand All @@ -53,6 +55,7 @@ go_library(
"//vendor/k8s.io/apimachinery/pkg/api/equality:go_default_library",
"//vendor/k8s.io/apimachinery/pkg/api/errors:go_default_library",
"//vendor/k8s.io/apimachinery/pkg/apis/meta/v1:go_default_library",
"//vendor/k8s.io/apimachinery/pkg/labels:go_default_library",
"//vendor/k8s.io/apimachinery/pkg/runtime:go_default_library",
"//vendor/k8s.io/apimachinery/pkg/runtime/schema:go_default_library",
"//vendor/k8s.io/apimachinery/pkg/types:go_default_library",
Expand All @@ -77,6 +80,7 @@ go_test(
"crds_test.go",
"delete_test.go",
"install_strategy_suite_test.go",
"instancetype_test.go",
"patches_test.go",
"pdb_test.go",
"prometheus_test.go",
Expand All @@ -96,6 +100,8 @@ go_test(
"//pkg/virt-operator/resource/generate/rbac:go_default_library",
"//pkg/virt-operator/util:go_default_library",
"//staging/src/kubevirt.io/api/core/v1:go_default_library",
"//staging/src/kubevirt.io/api/instancetype:go_default_library",
"//staging/src/kubevirt.io/api/instancetype/v1beta1:go_default_library",
"//staging/src/kubevirt.io/client-go/generated/kubevirt/clientset/versioned/fake:go_default_library",
"//staging/src/kubevirt.io/client-go/generated/prometheus-operator/clientset/versioned/fake:go_default_library",
"//staging/src/kubevirt.io/client-go/kubecli:go_default_library",
Expand All @@ -119,7 +125,9 @@ go_test(
"//vendor/k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1beta1:go_default_library",
"//vendor/k8s.io/apiextensions-apiserver/pkg/client/clientset/clientset/fake:go_default_library",
"//vendor/k8s.io/apimachinery/pkg/api/equality:go_default_library",
"//vendor/k8s.io/apimachinery/pkg/api/errors:go_default_library",
"//vendor/k8s.io/apimachinery/pkg/apis/meta/v1:go_default_library",
"//vendor/k8s.io/apimachinery/pkg/labels:go_default_library",
"//vendor/k8s.io/apimachinery/pkg/runtime:go_default_library",
"//vendor/k8s.io/apimachinery/pkg/util/intstr:go_default_library",
"//vendor/k8s.io/client-go/kubernetes/fake:go_default_library",
Expand Down
Loading

0 comments on commit 2823187

Please sign in to comment.