Skip to content

Commit

Permalink
Associate all kubevirt infra cluster resources with a cluster using a…
Browse files Browse the repository at this point in the history
…n infra id label

Signed-off-by: David Vossel <[email protected]>
  • Loading branch information
davidvossel committed Mar 2, 2023
1 parent 42e85b6 commit 5b3819f
Show file tree
Hide file tree
Showing 14 changed files with 103 additions and 16 deletions.
16 changes: 16 additions & 0 deletions api/v1beta1/hostedcluster_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,14 @@ const (
// SilenceClusterAlertsLabel is a label that can be used by consumers to indicate
// alerts from a cluster can be silenced or ignored
SilenceClusterAlertsLabel = "hypershift.openshift.io/silence-cluster-alerts"

// InfraIDLabel is a label that indicates the hosted cluster's infra id
// that the resource is associated with.
InfraIDLabel = "hypershift.openshift.io/infra-id"

// NodePoolNameLabel is a label that indicates the name of the node pool
// a resource is associated with
NodePoolNameLabel = "hypershift.openshift.io/nodepool-name"
)

// HostedClusterSpec is the desired behavior of a HostedCluster.
Expand Down Expand Up @@ -683,6 +691,14 @@ type KubevirtPlatformSpec struct {
// +optional
// +immutable
BaseDomainPassthrough *bool `json:"baseDomainPassthrough,omitempty"`

// GenerateID is used to uniquely apply a name postfix to resources associated with
// kubevirt infrastructure resources
// +kubebuilder:validation:Optional
// +kubebuilder:validation:XValidation:rule="self == oldSelf",message="Value is immutable"
// +kubebuilder:validation:MaxLength=11
// +optional
GenerateID string `json:"generateID,omitempty"`
}

// AgentPlatformSpec specifies configuration for agent-based installations.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6260,6 +6260,14 @@ spec:
Cluster: guest.apps.mgmt-cluster.example.com Apps: *.apps.guest.apps.mgmt-cluster.example.com
\n This is possible using OCP wildcard routes"
type: boolean
generateID:
description: GenerateID is used to uniquely apply a name postfix
to resources associated with kubevirt infrastructure resources
maxLength: 11
type: string
x-kubernetes-validations:
- message: Value is immutable
rule: self == oldSelf
type: object
powervs:
description: PowerVS specifies configuration for clusters running
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6249,6 +6249,14 @@ spec:
Cluster: guest.apps.mgmt-cluster.example.com Apps: *.apps.guest.apps.mgmt-cluster.example.com
\n This is possible using OCP wildcard routes"
type: boolean
generateID:
description: GenerateID is used to uniquely apply a name postfix
to resources associated with kubevirt infrastructure resources
maxLength: 11
type: string
x-kubernetes-validations:
- message: Value is immutable
rule: self == oldSelf
type: object
powervs:
description: PowerVS specifies configuration for clusters running
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package kubevirt

import (
hyperv1 "github.com/openshift/hypershift/api/v1beta1"
"gopkg.in/yaml.v2"
)

Expand All @@ -15,6 +16,7 @@ type CloudConfig struct {
LoadBalancer LoadBalancerConfig `yaml:"loadBalancer"`
InstancesV2 InstancesV2Config `yaml:"instancesV2"`
Namespace string `yaml:"namespace"`
InfraLabels map[string]string `yaml:"infraLabels"`
}

type LoadBalancerConfig struct {
Expand All @@ -39,7 +41,7 @@ func (c *CloudConfig) serialize() (string, error) {
return string(out), nil
}

func cloudConfig(namespace string) CloudConfig {
func cloudConfig(hcp *hyperv1.HostedControlPlane) CloudConfig {
return CloudConfig{
LoadBalancer: LoadBalancerConfig{
Enabled: true,
Expand All @@ -48,6 +50,9 @@ func cloudConfig(namespace string) CloudConfig {
Enabled: true,
ZoneAndRegionEnabled: false,
},
Namespace: namespace,
Namespace: hcp.Namespace,
InfraLabels: map[string]string{
hyperv1.InfraIDLabel: hcp.Spec.InfraID,
},
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import (
)

func ReconcileCloudConfig(cm *corev1.ConfigMap, hcp *hyperv1.HostedControlPlane) error {
cfg := cloudConfig(hcp.Namespace)
cfg := cloudConfig(hcp)
serializedCfg, err := cfg.serialize()
if err != nil {
return fmt.Errorf("failed to serialize cloudconfig: %w", err)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,10 +121,10 @@ func getContentsOrDie(file string) []byte {
return b
}

func reconcileInfraConfigMap(cm *corev1.ConfigMap) error {
func reconcileInfraConfigMap(cm *corev1.ConfigMap, infraID string) error {
cm.Data = map[string]string{
"infraClusterNamespace": cm.Namespace,
"infraClusterLabels": "",
"infraClusterLabels": fmt.Sprintf("%s=%s", hyperv1.InfraIDLabel, infraID),
}
return nil
}
Expand Down Expand Up @@ -420,7 +420,7 @@ func ReconcileInfra(client crclient.Client, hcp *hyperv1.HostedControlPlane, ctx

infraConfigMap := manifests.KubevirtCSIDriverInfraConfigMap(infraNamespace)
_, err = createOrUpdate(ctx, client, infraConfigMap, func() error {
return reconcileInfraConfigMap(infraConfigMap)
return reconcileInfraConfigMap(infraConfigMap, hcp.Spec.InfraID)
})
if err != nil {
return err
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,9 @@ func ReconcileDefaultIngressPassthroughService(service *corev1.Service, defaultN
return fmt.Errorf("unable to detect default ingress NodePort https port")
}

if service.Labels == nil {
service.Labels = map[string]string{}
}
service.Spec.Ports = []corev1.ServicePort{
{
Name: "https-443",
Expand All @@ -121,10 +124,13 @@ func ReconcileDefaultIngressPassthroughService(service *corev1.Service, defaultN
},
}
service.Spec.Selector = map[string]string{
"kubevirt.io": "virt-launcher",
"kubevirt.io": "virt-launcher",
hyperv1.InfraIDLabel: hcp.Spec.InfraID,
}
service.Spec.Type = corev1.ServiceTypeClusterIP

service.Labels[hyperv1.InfraIDLabel] = hcp.Spec.InfraID

ownerRef.ApplyTo(service)

return nil
Expand All @@ -133,6 +139,9 @@ func ReconcileDefaultIngressPassthroughService(service *corev1.Service, defaultN
func ReconcileDefaultIngressPassthroughRoute(route *routev1.Route, cpService *corev1.Service, hcp *hyperv1.HostedControlPlane) error {
ownerRef := config.OwnerRefFrom(hcp)

if route.Labels == nil {
route.Labels = map[string]string{}
}
route.Spec.WildcardPolicy = routev1.WildcardPolicySubdomain
route.Spec.Host = fmt.Sprintf("https.apps.%s.%s", hcp.Name, hcp.Spec.DNS.BaseDomain)
route.Spec.TLS = &routev1.TLSConfig{
Expand All @@ -142,6 +151,7 @@ func ReconcileDefaultIngressPassthroughRoute(route *routev1.Route, cpService *co
Kind: "Service",
Name: cpService.Name,
}
route.Labels[hyperv1.InfraIDLabel] = hcp.Spec.InfraID

ownerRef.ApplyTo(route)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,19 +35,21 @@ func IngressDefaultIngressNodePortService() *corev1.Service {
}
}

const IngressDefaultIngressPassthroughServiceName = "default-ingress-passthrough-service"

func IngressDefaultIngressPassthroughService(namespace string) *corev1.Service {
return &corev1.Service{
ObjectMeta: metav1.ObjectMeta{
Name: "default-ingress-passthrough-service",
Namespace: namespace,
},
}
}

const IngressDefaultIngressPassthroughRouteName = "default-ingress-passthrough-route"

func IngressDefaultIngressPassthroughRoute(namespace string) *routev1.Route {
return &routev1.Route{
ObjectMeta: metav1.ObjectMeta{
Name: "default-ingress-passthrough-route",
Namespace: namespace,
},
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -775,9 +775,17 @@ func (r *reconciler) reconcileIngressController(ctx context.Context, hcp *hyperv
// Manifests for infra/mgmt cluster passthrough service
cpService := manifests.IngressDefaultIngressPassthroughService(hcpNamespace)

cpService.Name = fmt.Sprintf("%s-%s",
manifests.IngressDefaultIngressPassthroughServiceName,
hcp.Spec.Platform.Kubevirt.GenerateID)

// Manifests for infra/mgmt cluster passthrough routes
cpPassthroughRoute := manifests.IngressDefaultIngressPassthroughRoute(hcpNamespace)

cpPassthroughRoute.Name = fmt.Sprintf("%s-%s",
manifests.IngressDefaultIngressPassthroughRouteName,
hcp.Spec.Platform.Kubevirt.GenerateID)

if _, err := r.CreateOrUpdate(ctx, r.cpClient, cpService, func() error {
return ingress.ReconcileDefaultIngressPassthroughService(cpService, defaultIngressNodePortService, hcp)
}); err != nil {
Expand Down
13 changes: 13 additions & 0 deletions docs/content/reference/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -4944,6 +4944,19 @@ Apps: *.apps.guest.apps.mgmt-cluster.example.com</p>
<p>This is possible using OCP wildcard routes</p>
</td>
</tr>
<tr>
<td>
<code>generateID</code></br>
<em>
string
</em>
</td>
<td>
<em>(Optional)</em>
<p>GenerateID is used to uniquely apply a name postfix to resources associated with
kubevirt infrastructure resources</p>
</td>
</tr>
</tbody>
</table>
###KubevirtRootVolume { #hypershift.openshift.io/v1beta1.KubevirtRootVolume }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ import (
"k8s.io/apimachinery/pkg/types"
utilerrors "k8s.io/apimachinery/pkg/util/errors"
"k8s.io/apimachinery/pkg/util/intstr"
utilrand "k8s.io/apimachinery/pkg/util/rand"
"k8s.io/apimachinery/pkg/util/sets"
"k8s.io/client-go/util/workqueue"
"k8s.io/utils/clock"
Expand Down Expand Up @@ -4331,7 +4332,11 @@ func (r *HostedClusterReconciler) serviceAccountSigningKeyBytes(ctx context.Cont

func (r *HostedClusterReconciler) reconcileKubevirtPlatformDefaultSettings(ctx context.Context, hc *hyperv1.HostedCluster) error {
if hc.Spec.Platform.Kubevirt == nil {
return nil
hc.Spec.Platform.Kubevirt = &hyperv1.KubevirtPlatformSpec{}
}

if hc.Spec.Platform.Kubevirt.GenerateID == "" {
hc.Spec.Platform.Kubevirt.GenerateID = utilrand.String(10)
}

// auto generate the basedomain by retrieving the default ingress *.apps dns.
Expand Down
8 changes: 6 additions & 2 deletions hypershift-operator/controllers/nodepool/kubevirt/kubevirt.go
Original file line number Diff line number Diff line change
Expand Up @@ -201,8 +201,9 @@ func virtualMachineTemplateBase(image string, kvPlatform *hyperv1.KubevirtNodePo
return template
}

func MachineTemplateSpec(image string, nodePool *hyperv1.NodePool) *capikubevirt.KubevirtMachineTemplateSpec {
nodePoolNameLabelKey := "hypershift.kubevirt.io/node-pool-name"
func MachineTemplateSpec(image string, nodePool *hyperv1.NodePool, hc *hyperv1.HostedCluster) *capikubevirt.KubevirtMachineTemplateSpec {
nodePoolNameLabelKey := hyperv1.NodePoolNameLabel
infraIDLabelKey := hyperv1.InfraIDLabel

vmTemplate := virtualMachineTemplateBase(image, nodePool.Spec.Platform.Kubevirt)

Expand Down Expand Up @@ -236,7 +237,10 @@ func MachineTemplateSpec(image string, nodePool *hyperv1.NodePool) *capikubevirt
}

vmTemplate.Spec.Template.ObjectMeta.Labels[nodePoolNameLabelKey] = nodePool.Name
vmTemplate.Spec.Template.ObjectMeta.Labels[infraIDLabelKey] = hc.Spec.InfraID

vmTemplate.ObjectMeta.Labels[nodePoolNameLabelKey] = nodePool.Name
vmTemplate.ObjectMeta.Labels[infraIDLabelKey] = hc.Spec.InfraID

return &capikubevirt.KubevirtMachineTemplateSpec{
Template: capikubevirt.KubevirtMachineTemplateResource{
Expand Down
14 changes: 11 additions & 3 deletions hypershift-operator/controllers/nodepool/kubevirt/kubevirt_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ func TestKubevirtMachineTemplate(t *testing.T) {
testCases := []struct {
name string
nodePool *hyperv1.NodePool
hc *hyperv1.HostedCluster
expected *capikubevirt.KubevirtMachineTemplateSpec
}{
{
Expand All @@ -40,7 +41,11 @@ func TestKubevirtMachineTemplate(t *testing.T) {
Release: hyperv1.Release{},
},
},

hc: &hyperv1.HostedCluster{
Spec: hyperv1.HostedClusterSpec{
InfraID: "1234",
},
},
expected: &capikubevirt.KubevirtMachineTemplateSpec{
Template: capikubevirt.KubevirtMachineTemplateResource{
Spec: capikubevirt.KubevirtMachineSpec{
Expand All @@ -57,7 +62,7 @@ func TestKubevirtMachineTemplate(t *testing.T) {
err := PlatformValidation(tc.nodePool)
g.Expect(err).ToNot(HaveOccurred())

result := MachineTemplateSpec("", tc.nodePool)
result := MachineTemplateSpec("", tc.nodePool, tc.hc)
if !equality.Semantic.DeepEqual(tc.expected, result) {
t.Errorf(cmp.Diff(tc.expected, result))
}
Expand Down Expand Up @@ -95,13 +100,15 @@ func generateNodeTemplate(memory string, cpu uint32, image string, volumeSize st
runAlways := kubevirtv1.RunStrategyAlways
guestQuantity := apiresource.MustParse(memory)
volumeSizeQuantity := apiresource.MustParse(volumeSize)
nodePoolNameLabelKey := "hypershift.kubevirt.io/node-pool-name"
nodePoolNameLabelKey := hyperv1.NodePoolNameLabel
infraIDLabelKey := hyperv1.InfraIDLabel
pullMethod := v1beta1.RegistryPullNode

return &capikubevirt.VirtualMachineTemplateSpec{
ObjectMeta: metav1.ObjectMeta{
Labels: map[string]string{
nodePoolNameLabelKey: "my-pool",
infraIDLabelKey: "1234",
},
},
Spec: kubevirtv1.VirtualMachineSpec{
Expand Down Expand Up @@ -133,6 +140,7 @@ func generateNodeTemplate(memory string, cpu uint32, image string, volumeSize st
ObjectMeta: metav1.ObjectMeta{
Labels: map[string]string{
nodePoolNameLabelKey: "my-pool",
infraIDLabelKey: "1234",
},
},
Spec: kubevirtv1.VirtualMachineInstanceSpec{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2238,7 +2238,7 @@ func machineTemplateBuilders(hcluster *hyperv1.HostedCluster, nodePool *hyperv1.
}
case hyperv1.KubevirtPlatform:
template = &capikubevirt.KubevirtMachineTemplate{}
machineTemplateSpec = kubevirt.MachineTemplateSpec(kubevirtBootImage, nodePool)
machineTemplateSpec = kubevirt.MachineTemplateSpec(kubevirtBootImage, nodePool, hcluster)
mutateTemplate = func(object client.Object) error {
o, _ := object.(*capikubevirt.KubevirtMachineTemplate)
o.Spec = *machineTemplateSpec.(*capikubevirt.KubevirtMachineTemplateSpec)
Expand Down

0 comments on commit 5b3819f

Please sign in to comment.