Skip to content

Commit

Permalink
Move KubeVirt CR creation function to the resource generator
Browse files Browse the repository at this point in the history
and add missing error checks

Signed-off-by: Jed Lejosne <[email protected]>
  • Loading branch information
jean-edouard committed May 26, 2022
1 parent 7942f32 commit ad75f98
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 37 deletions.
32 changes: 0 additions & 32 deletions pkg/virt-operator/resource/generate/components/crds.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ import (

migrationsv1 "kubevirt.io/api/migrations/v1alpha1"

corev1 "k8s.io/api/core/v1"
schedulingv1 "k8s.io/api/scheduling/v1"
extv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
Expand Down Expand Up @@ -665,37 +664,6 @@ func NewMigrationPolicyCrd() (*extv1.CustomResourceDefinition, error) {
return crd, nil
}

// Used by manifest generation
func NewKubeVirtCR(namespace string, pullPolicy corev1.PullPolicy, featureGates string, infraReplicas uint8) *virtv1.KubeVirt {
cr := &virtv1.KubeVirt{
TypeMeta: metav1.TypeMeta{
APIVersion: virtv1.GroupVersion.String(),
Kind: "KubeVirt",
},
ObjectMeta: metav1.ObjectMeta{
Namespace: namespace,
Name: "kubevirt",
},
Spec: virtv1.KubeVirtSpec{
ImagePullPolicy: pullPolicy,
},
}

if featureGates != "" {
cr.Spec.Configuration = virtv1.KubeVirtConfiguration{
DeveloperConfiguration: &virtv1.DeveloperConfiguration{
FeatureGates: strings.Split(featureGates, ","),
},
}
}

cr.Spec.Infra = &virtv1.ComponentConfig{
Replicas: &infraReplicas,
}

return cr
}

// NewKubeVirtPriorityClassCR is used for manifest generation
func NewKubeVirtPriorityClassCR() *schedulingv1.PriorityClass {
return &schedulingv1.PriorityClass{
Expand Down
2 changes: 2 additions & 0 deletions tools/resource-generator/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,10 @@ go_library(
deps = [
"//pkg/virt-operator/resource/generate/components:go_default_library",
"//pkg/virt-operator/resource/generate/rbac:go_default_library",
"//staging/src/kubevirt.io/api/core/v1:go_default_library",
"//tools/util:go_default_library",
"//vendor/k8s.io/api/core/v1:go_default_library",
"//vendor/k8s.io/apimachinery/pkg/apis/meta/v1:go_default_library",
],
)

Expand Down
56 changes: 51 additions & 5 deletions tools/resource-generator/resource-generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ import (
"strconv"
"strings"

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

virtv1 "kubevirt.io/api/core/v1"

v1 "k8s.io/api/core/v1"

"kubevirt.io/kubevirt/pkg/virt-operator/resource/generate/components"
Expand All @@ -40,6 +44,36 @@ const (
infraReplicasPlaceholder = 255
)

func newKubeVirtCR(namespace string, pullPolicy v1.PullPolicy, featureGates string, infraReplicas uint8) *virtv1.KubeVirt {
cr := &virtv1.KubeVirt{
TypeMeta: metav1.TypeMeta{
APIVersion: virtv1.GroupVersion.String(),
Kind: "KubeVirt",
},
ObjectMeta: metav1.ObjectMeta{
Namespace: namespace,
Name: "kubevirt",
},
Spec: virtv1.KubeVirtSpec{
ImagePullPolicy: pullPolicy,
},
}

if featureGates != "" {
cr.Spec.Configuration = virtv1.KubeVirtConfiguration{
DeveloperConfiguration: &virtv1.DeveloperConfiguration{
FeatureGates: strings.Split(featureGates, ","),
},
}
}

cr.Spec.Infra = &virtv1.ComponentConfig{
Replicas: &infraReplicas,
}

return cr
}

func generateKubeVirtCR(namespace *string, imagePullPolicy v1.PullPolicy, featureGatesFlag *string, infraReplicasFlag *string) {
var featureGates string
if strings.HasPrefix(*featureGatesFlag, "{{") {
Expand All @@ -58,7 +92,10 @@ func generateKubeVirtCR(namespace *string, imagePullPolicy v1.PullPolicy, featur
infraReplicas = uint8(val)
}
var buf bytes.Buffer
util.MarshallObject(components.NewKubeVirtCR(*namespace, imagePullPolicy, featureGates, infraReplicas), &buf)
err := util.MarshallObject(newKubeVirtCR(*namespace, imagePullPolicy, featureGates, infraReplicas), &buf)
if err != nil {
panic(err)
}
cr := buf.String()
// When creating a template, we need to add code to iterate over the feature-gates slice variable.
// util.MarshallObject(), called above, uses yaml.Marshall(), which can only generate valid yaml.
Expand Down Expand Up @@ -111,19 +148,28 @@ func main() {
case "kv":
kv, err := components.NewKubeVirtCrd()
if err != nil {
panic(fmt.Errorf("This should not happen, %v", err))
panic(fmt.Errorf("this should not happen, %v", err))
}
err = util.MarshallObject(kv, os.Stdout)
if err != nil {
panic(err)
}
util.MarshallObject(kv, os.Stdout)
case "kv-cr":
generateKubeVirtCR(namespace, imagePullPolicy, featureGates, infraReplicas)
case "operator-rbac":
all := rbac.GetAllOperator(*namespace)
for _, r := range all {
util.MarshallObject(r, os.Stdout)
err := util.MarshallObject(r, os.Stdout)
if err != nil {
panic(err)
}
}
case "priorityclass":
priorityClass := components.NewKubeVirtPriorityClassCR()
util.MarshallObject(priorityClass, os.Stdout)
err := util.MarshallObject(priorityClass, os.Stdout)
if err != nil {
panic(err)
}
default:
panic(fmt.Errorf("unknown resource type %s", *resourceType))
}
Expand Down

0 comments on commit ad75f98

Please sign in to comment.