Skip to content

Commit

Permalink
Add the possibility to set default feature gates
Browse files Browse the repository at this point in the history
Signed-off-by: Jed Lejosne <[email protected]>
  • Loading branch information
jean-edouard committed Mar 29, 2021
1 parent e7fa484 commit 759694b
Show file tree
Hide file tree
Showing 8 changed files with 54 additions and 8 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ build-verify:

manifests:
hack/dockerized "CSV_VERSION=${CSV_VERSION} QUAY_REPOSITORY=${QUAY_REPOSITORY} \
DOCKER_PREFIX=${DOCKER_PREFIX} DOCKER_TAG=${DOCKER_TAG} \
DOCKER_PREFIX=${DOCKER_PREFIX} DOCKER_TAG=${DOCKER_TAG} KUBEVIRT_PROVIDER=${KUBEVIRT_PROVIDER} \
IMAGE_PULL_POLICY=${IMAGE_PULL_POLICY} VERBOSITY=${VERBOSITY} PACKAGE_NAME=${PACKAGE_NAME} \
KUBEVIRT_INSTALLED_NAMESPACE=${KUBEVIRT_INSTALLED_NAMESPACE} ./hack/build-manifests.sh"

Expand Down
2 changes: 2 additions & 0 deletions hack/build-manifests.sh
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ for arg in $args; do
--virt-controller-sha=${VIRT_CONTROLLER_SHA} \
--virt-handler-sha=${VIRT_HANDLER_SHA} \
--virt-launcher-sha=${VIRT_LAUNCHER_SHA} \
--feature-gates=${feature_gates} \
>${outfile}

if [ "$skipj2" = true ]; then
Expand All @@ -116,6 +117,7 @@ for arg in $args; do
--package-name=${package_name} \
--input-file=${infile} \
--quay-repository=${QUAY_REPOSITORY} \
--feature-gates=${feature_gates} \
>${template_outfile}
done

Expand Down
4 changes: 2 additions & 2 deletions hack/generate.sh
Original file line number Diff line number Diff line change
Expand Up @@ -115,8 +115,8 @@ rm -f ${KUBEVIRT_DIR}/examples/*
ResourceDir=${KUBEVIRT_DIR}/manifests/generated
${KUBEVIRT_DIR}/tools/resource-generator/resource-generator --type=priorityclass >${ResourceDir}/kubevirt-priority-class.yaml
${KUBEVIRT_DIR}/tools/resource-generator/resource-generator --type=kv >${ResourceDir}/kv-resource.yaml
${KUBEVIRT_DIR}/tools/resource-generator/resource-generator --type=kv-cr --namespace={{.Namespace}} --pullPolicy={{.ImagePullPolicy}} >${ResourceDir}/kubevirt-cr.yaml.in
${KUBEVIRT_DIR}/tools/resource-generator/resource-generator --type=operator-rbac --namespace={{.Namespace}} >${ResourceDir}/rbac-operator.authorization.k8s.yaml.in
${KUBEVIRT_DIR}/tools/resource-generator/resource-generator --type=kv-cr --namespace='{{.Namespace}}' --pullPolicy='{{.ImagePullPolicy}}' --featureGates='{{.FeatureGates}}' >${ResourceDir}/kubevirt-cr.yaml.in
${KUBEVIRT_DIR}/tools/resource-generator/resource-generator --type=operator-rbac --namespace='{{.Namespace}}' >${ResourceDir}/rbac-operator.authorization.k8s.yaml.in

# used for Image fields in manifests
function getVersion() {
Expand Down
7 changes: 6 additions & 1 deletion manifests/generated/kubevirt-cr.yaml.in
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,12 @@ metadata:
namespace: {{.Namespace}}
spec:
certificateRotateStrategy: {}
configuration: {}
configuration:
developerConfiguration:
featureGates:{{if .FeatureGates}}
{{- range .FeatureGates}}
- {{.}}
{{- end}}{{else}} []{{end}}
customizeComponents: {}
imagePullPolicy: {{.ImagePullPolicy}}
workloadUpdateStrategy: {}
14 changes: 12 additions & 2 deletions pkg/virt-operator/resource/generate/components/crds.go
Original file line number Diff line number Diff line change
Expand Up @@ -803,8 +803,8 @@ func NewPrometheusRuleSpec(ns string, workloadUpdatesEnabled bool) *promv1.Prome
}

// Used by manifest generation
func NewKubeVirtCR(namespace string, pullPolicy corev1.PullPolicy) *virtv1.KubeVirt {
return &virtv1.KubeVirt{
func NewKubeVirtCR(namespace string, pullPolicy corev1.PullPolicy, featureGates string) *virtv1.KubeVirt {
cr := &virtv1.KubeVirt{
TypeMeta: metav1.TypeMeta{
APIVersion: virtv1.GroupVersion.String(),
Kind: "KubeVirt",
Expand All @@ -817,6 +817,16 @@ func NewKubeVirtCR(namespace string, pullPolicy corev1.PullPolicy) *virtv1.KubeV
ImagePullPolicy: pullPolicy,
},
}

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

return cr
}

// NewKubeVirtPriorityClassCR is used for manifest generation
Expand Down
5 changes: 5 additions & 0 deletions tools/manifest-templator/manifest-templator.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ type templateData struct {
VirtHandlerSha string
VirtLauncherSha string
PriorityClassSpec string
FeatureGates []string
GeneratedManifests map[string]string
}

Expand All @@ -91,6 +92,7 @@ func main() {
virtControllerSha := flag.String("virt-controller-sha", "", "")
virtHandlerSha := flag.String("virt-handler-sha", "", "")
virtLauncherSha := flag.String("virt-launcher-sha", "", "")
featureGates := flag.String("feature-gates", "", "")

pflag.CommandLine.AddGoFlagSet(flag.CommandLine)
pflag.CommandLine.ParseErrorsWhitelist.UnknownFlags = true
Expand Down Expand Up @@ -133,6 +135,9 @@ func main() {
data.ReplacesCsvVersion = ""
data.OperatorDeploymentSpec = getOperatorDeploymentSpec(data, 2)
data.PriorityClassSpec = getPriorityClassSpec(2)
if *featureGates != "" {
data.FeatureGates = strings.Split(*featureGates, ",")
}

// operator deployment differs a bit in normal manifest and CSV
if strings.Contains(*inputFile, ".clusterserviceversion.yaml") {
Expand Down
5 changes: 3 additions & 2 deletions tools/resource-generator/resource-generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,10 @@ import (
)

func main() {
resourceType := flag.String("type", "", "Type of resource to generate. vmi | vmipreset | vmirs | vm | vmim | kv | rbac | priorityclass")
resourceType := flag.String("type", "", "Type of resource to generate. kv | kv-cr | operator-rbac | priorityclass")
namespace := flag.String("namespace", "kube-system", "Namespace to use.")
pullPolicy := flag.String("pullPolicy", "IfNotPresent", "ImagePullPolicy to use.")
featureGates := flag.String("featureGates", "", "Feature gates to enable.")

flag.Parse()

Expand All @@ -48,7 +49,7 @@ func main() {
}
util.MarshallObject(kv, os.Stdout)
case "kv-cr":
util.MarshallObject(components.NewKubeVirtCR(*namespace, imagePullPolicy), os.Stdout)
util.MarshallObject(components.NewKubeVirtCR(*namespace, imagePullPolicy, *featureGates), os.Stdout)
case "operator-rbac":
all := rbac.GetAllOperator(*namespace)
for _, r := range all {
Expand Down
23 changes: 23 additions & 0 deletions tools/util/marshaller.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ package util
import (
"encoding/json"
"io"
"regexp"
"strings"

v1 "kubevirt.io/client-go/api/v1"
Expand Down Expand Up @@ -128,6 +129,28 @@ func MarshallObject(obj interface{}, writer io.Writer) error {
s = strings.Replace(s, " '\"", " \"", -1)
s = strings.Replace(s, "\"'\n", "\"\n", -1)

// The current function is sometimes used on yaml templates, and manipulates them as json/yaml above.
// However, this only works for simple templates dealing with simple strings.
// For list values, we need template code to iterate over the slice, and that code is not valid yaml.
// To work around that, the variable name for the featureGates slice was treated as the first and only list item until now.
// Therefore, if we're currently handling a template, the featureGates section looks like:
// featureGates:
// - {{.FeatureGates}}
// however we want to treat the variable (".FeatureGates" here) as a slice and iterate over it (with a special case for empty list):
// featureGates:{{if .FeatureGates}}
// {{- range .FeatureGates}}
// - {{.}}
// {{- end}}{{else}} []{{end}}
// The replace call below will transform the former into the latter, keeping the variable name ($2) and intendation ($1)
featureGates, exists, err := unstructured.NestedStringSlice(r.Object, "spec", "configuration", "developerConfiguration", "featureGates")
if err == nil && exists && len(featureGates) == 1 && strings.HasPrefix(featureGates[0], `{{`) {
re := regexp.MustCompile(`(?m)featureGates:\n([ \t]+)- \{\{(.*)\}\}`)
s = re.ReplaceAllString(s, `featureGates:{{if $2}}
$1{{- range $2}}
$1- {{.}}
$1{{- end}}{{else}} []{{end}}`)
}

yamlBytes = []byte(s)

_, err = writer.Write([]byte("---\n"))
Expand Down

0 comments on commit 759694b

Please sign in to comment.