sigs.k8s.io/cluster-api@v1.7.1/exp/topology/scope/state.go (about) 1 /* 2 Copyright 2021 The Kubernetes Authors. 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 package scope 18 19 import ( 20 "context" 21 22 "github.com/pkg/errors" 23 "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" 24 "sigs.k8s.io/controller-runtime/pkg/client" 25 26 clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1" 27 expv1 "sigs.k8s.io/cluster-api/exp/api/v1beta1" 28 "sigs.k8s.io/cluster-api/internal/topology/check" 29 ) 30 31 // ClusterState holds all the objects representing the state of a managed Cluster topology. 32 // NOTE: please note that we are going to deal with two different type state, the current state as read from the API server, 33 // and the desired state resulting from processing the ClusterBlueprint. 34 type ClusterState struct { 35 // Cluster holds the Cluster object. 36 Cluster *clusterv1.Cluster 37 38 // InfrastructureCluster holds the infrastructure cluster object referenced by the Cluster. 39 InfrastructureCluster *unstructured.Unstructured 40 41 // ControlPlane holds the controlplane object referenced by the Cluster. 42 ControlPlane *ControlPlaneState 43 44 // MachineDeployments holds the machine deployments in the Cluster. 45 MachineDeployments MachineDeploymentsStateMap 46 47 // MachinePools holds the MachinePools in the Cluster. 48 MachinePools MachinePoolsStateMap 49 } 50 51 // ControlPlaneState holds all the objects representing the state of a managed control plane. 52 type ControlPlaneState struct { 53 // Object holds the ControlPlane object. 54 Object *unstructured.Unstructured 55 56 // InfrastructureMachineTemplate holds the infrastructure template referenced by the ControlPlane object. 57 InfrastructureMachineTemplate *unstructured.Unstructured 58 59 // MachineHealthCheckClass holds the MachineHealthCheck for this ControlPlane. 60 // +optional 61 MachineHealthCheck *clusterv1.MachineHealthCheck 62 } 63 64 // MachineDeploymentsStateMap holds a collection of MachineDeployment states. 65 type MachineDeploymentsStateMap map[string]*MachineDeploymentState 66 67 // Upgrading returns the list of the machine deployments 68 // that are upgrading. 69 func (mds MachineDeploymentsStateMap) Upgrading(ctx context.Context, c client.Reader) ([]string, error) { 70 names := []string{} 71 for _, md := range mds { 72 upgrading, err := md.IsUpgrading(ctx, c) 73 if err != nil { 74 return nil, errors.Wrap(err, "failed to list upgrading MachineDeployments") 75 } 76 if upgrading { 77 names = append(names, md.Object.Name) 78 } 79 } 80 return names, nil 81 } 82 83 // MachineDeploymentState holds all the objects representing the state of a managed deployment. 84 type MachineDeploymentState struct { 85 // Object holds the MachineDeployment object. 86 Object *clusterv1.MachineDeployment 87 88 // BootstrapTemplate holds the bootstrap template referenced by the MachineDeployment object. 89 BootstrapTemplate *unstructured.Unstructured 90 91 // InfrastructureMachineTemplate holds the infrastructure machine template referenced by the MachineDeployment object. 92 InfrastructureMachineTemplate *unstructured.Unstructured 93 94 // MachineHealthCheck holds a MachineHealthCheck linked to the MachineDeployment object. 95 // +optional 96 MachineHealthCheck *clusterv1.MachineHealthCheck 97 } 98 99 // IsUpgrading determines if the MachineDeployment is upgrading. 100 // A machine deployment is considered upgrading if at least one of the Machines of this 101 // MachineDeployment has a different version. 102 func (md *MachineDeploymentState) IsUpgrading(ctx context.Context, c client.Reader) (bool, error) { 103 return check.IsMachineDeploymentUpgrading(ctx, c, md.Object) 104 } 105 106 // MachinePoolsStateMap holds a collection of MachinePool states. 107 type MachinePoolsStateMap map[string]*MachinePoolState 108 109 // Upgrading returns the list of the machine pools 110 // that are upgrading. 111 func (mps MachinePoolsStateMap) Upgrading(ctx context.Context, c client.Reader) ([]string, error) { 112 names := []string{} 113 for _, mp := range mps { 114 upgrading, err := mp.IsUpgrading(ctx, c) 115 if err != nil { 116 return nil, errors.Wrap(err, "failed to list upgrading MachinePools") 117 } 118 if upgrading { 119 names = append(names, mp.Object.Name) 120 } 121 } 122 return names, nil 123 } 124 125 // MachinePoolState holds all the objects representing the state of a managed pool. 126 type MachinePoolState struct { 127 // Object holds the MachinePool object. 128 Object *expv1.MachinePool 129 130 // BootstrapObject holds the MachinePool bootstrap object. 131 BootstrapObject *unstructured.Unstructured 132 133 // InfrastructureMachinePoolObject holds the infrastructure machine template referenced by the MachinePool object. 134 InfrastructureMachinePoolObject *unstructured.Unstructured 135 } 136 137 // IsUpgrading determines if the MachinePool is upgrading. 138 // A machine pool is considered upgrading if at least one of the Machines of this 139 // MachinePool has a different version. 140 func (mp *MachinePoolState) IsUpgrading(ctx context.Context, c client.Reader) (bool, error) { 141 return check.IsMachinePoolUpgrading(ctx, c, mp.Object) 142 }