|
| 1 | +/* |
| 2 | +Copyright 2020 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 | + "fmt" |
| 21 | + |
| 22 | + awsclient "github.com/aws/aws-sdk-go/aws/client" |
| 23 | + "github.com/go-logr/logr" |
| 24 | + "github.com/pkg/errors" |
| 25 | + "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" |
| 26 | + "k8s.io/klog/klogr" |
| 27 | + infrav1 "sigs.k8s.io/cluster-api-provider-aws/api/v1alpha3" |
| 28 | + "sigs.k8s.io/cluster-api-provider-aws/pkg/cloud" |
| 29 | + clusterv1 "sigs.k8s.io/cluster-api/api/v1alpha3" |
| 30 | + "sigs.k8s.io/cluster-api/util/patch" |
| 31 | + "sigs.k8s.io/controller-runtime/pkg/client" |
| 32 | +) |
| 33 | + |
| 34 | +// ExternalInfraClusterScopeParams defines the input parameters used to create a new Scope. |
| 35 | +type ExternalInfraClusterScopeParams struct { |
| 36 | + Client client.Client |
| 37 | + Logger logr.Logger |
| 38 | + Cluster *clusterv1.Cluster |
| 39 | + ExternalInfraCluster *unstructured.Unstructured |
| 40 | + ControllerName string |
| 41 | + Endpoints []ServiceEndpoint |
| 42 | + Session awsclient.ConfigProvider |
| 43 | +} |
| 44 | + |
| 45 | +// NewExternalInfraClusterScope creates a new Scope from the supplied parameters. |
| 46 | +// This is meant to be called for each reconcile iteration. |
| 47 | +func NewExternalInfraClusterScope(params ExternalInfraClusterScopeParams) (*ExternalInfraClusterScope, error) { |
| 48 | + if params.Cluster == nil { |
| 49 | + return nil, errors.New("failed to generate new scope from nil Cluster") |
| 50 | + } |
| 51 | + if params.ExternalInfraCluster == nil { |
| 52 | + return nil, errors.New("failed to generate new scope from nil ExternalInfraCluster") |
| 53 | + } |
| 54 | + |
| 55 | + if params.Logger == nil { |
| 56 | + params.Logger = klogr.New() |
| 57 | + } |
| 58 | + |
| 59 | + region, found, err := unstructured.NestedString(params.ExternalInfraCluster.Object, "spec", "region") |
| 60 | + if err != nil || !found { |
| 61 | + return nil, fmt.Errorf("error getting region: %w", err) |
| 62 | + } |
| 63 | + session, err := sessionForRegion(region, params.Endpoints) |
| 64 | + if err != nil { |
| 65 | + return nil, errors.Errorf("failed to create aws session: %v", err) |
| 66 | + } |
| 67 | + |
| 68 | + helper, err := patch.NewHelper(params.ExternalInfraCluster, params.Client) |
| 69 | + if err != nil { |
| 70 | + return nil, errors.Wrap(err, "failed to init patch helper") |
| 71 | + } |
| 72 | + return &ExternalInfraClusterScope{ |
| 73 | + Logger: params.Logger, |
| 74 | + client: params.Client, |
| 75 | + Cluster: params.Cluster, |
| 76 | + ExternalInfraCluster: &ExternalInfraClusterObject{params.ExternalInfraCluster}, |
| 77 | + patchHelper: helper, |
| 78 | + session: session, |
| 79 | + controllerName: params.ControllerName, |
| 80 | + }, nil |
| 81 | +} |
| 82 | + |
| 83 | +// ExternalInfraClusterScope defines the basic context for an actuator to operate upon. |
| 84 | +type ExternalInfraClusterScope struct { |
| 85 | + logr.Logger |
| 86 | + client client.Client |
| 87 | + patchHelper *patch.Helper |
| 88 | + |
| 89 | + Cluster *clusterv1.Cluster |
| 90 | + ExternalInfraCluster *ExternalInfraClusterObject |
| 91 | + |
| 92 | + session awsclient.ConfigProvider |
| 93 | + controllerName string |
| 94 | +} |
| 95 | + |
| 96 | +// Network returns the cluster network object. |
| 97 | +func (s *ExternalInfraClusterScope) Network() *infrav1.Network { |
| 98 | + return nil |
| 99 | +} |
| 100 | + |
| 101 | +// VPC returns the cluster VPC. |
| 102 | +func (s *ExternalInfraClusterScope) VPC() *infrav1.VPCSpec { |
| 103 | + return &infrav1.VPCSpec{} |
| 104 | +} |
| 105 | + |
| 106 | +// Subnets returns the cluster subnets. |
| 107 | +func (s *ExternalInfraClusterScope) Subnets() infrav1.Subnets { |
| 108 | + return nil |
| 109 | +} |
| 110 | + |
| 111 | +// SetSubnets updates the clusters subnets. |
| 112 | +func (s *ExternalInfraClusterScope) SetSubnets(subnets infrav1.Subnets) { |
| 113 | +} |
| 114 | + |
| 115 | +// CNIIngressRules returns the CNI spec ingress rules. |
| 116 | +func (s *ExternalInfraClusterScope) CNIIngressRules() infrav1.CNIIngressRules { |
| 117 | + return infrav1.CNIIngressRules{} |
| 118 | +} |
| 119 | + |
| 120 | +// SecurityGroups returns the cluster security groups as a map, it creates the map if empty. |
| 121 | +func (s *ExternalInfraClusterScope) SecurityGroups() map[infrav1.SecurityGroupRole]infrav1.SecurityGroup { |
| 122 | + return nil |
| 123 | +} |
| 124 | + |
| 125 | +// Name returns the CAPI cluster name. |
| 126 | +func (s *ExternalInfraClusterScope) Name() string { |
| 127 | + return s.Cluster.Name |
| 128 | +} |
| 129 | + |
| 130 | +// Namespace returns the cluster namespace. |
| 131 | +func (s *ExternalInfraClusterScope) Namespace() string { |
| 132 | + return s.Cluster.Namespace |
| 133 | +} |
| 134 | + |
| 135 | +// Region returns the cluster region. |
| 136 | +func (s *ExternalInfraClusterScope) Region() string { |
| 137 | + region, found, err := unstructured.NestedString(s.ExternalInfraCluster.Object, "spec", "region") |
| 138 | + if err != nil || !found { |
| 139 | + s.Error(err, "error getting region") |
| 140 | + return "" |
| 141 | + } |
| 142 | + return region |
| 143 | +} |
| 144 | + |
| 145 | +// KubernetesClusterName is the name of the Kubernetes cluster. For the cluster |
| 146 | +// scope this is the same as the CAPI cluster name |
| 147 | +func (s *ExternalInfraClusterScope) KubernetesClusterName() string { |
| 148 | + return s.Cluster.Name |
| 149 | +} |
| 150 | + |
| 151 | +// ControlPlaneLoadBalancer returns the AWSLoadBalancerSpec |
| 152 | +func (s *ExternalInfraClusterScope) ControlPlaneLoadBalancer() *infrav1.AWSLoadBalancerSpec { |
| 153 | + return nil |
| 154 | +} |
| 155 | + |
| 156 | +// ControlPlaneLoadBalancerScheme returns the Classic ELB scheme (public or internal facing) |
| 157 | +func (s *ExternalInfraClusterScope) ControlPlaneLoadBalancerScheme() infrav1.ClassicELBScheme { |
| 158 | + if s.ControlPlaneLoadBalancer() != nil && s.ControlPlaneLoadBalancer().Scheme != nil { |
| 159 | + return *s.ControlPlaneLoadBalancer().Scheme |
| 160 | + } |
| 161 | + return infrav1.ClassicELBSchemeInternetFacing |
| 162 | +} |
| 163 | + |
| 164 | +// ControlPlaneConfigMapName returns the name of the ConfigMap used to |
| 165 | +// coordinate the bootstrapping of control plane nodes. |
| 166 | +func (s *ExternalInfraClusterScope) ControlPlaneConfigMapName() string { |
| 167 | + return fmt.Sprintf("%s-controlplane", s.Cluster.UID) |
| 168 | +} |
| 169 | + |
| 170 | +// ListOptionsLabelSelector returns a ListOptions with a label selector for clusterName. |
| 171 | +func (s *ExternalInfraClusterScope) ListOptionsLabelSelector() client.ListOption { |
| 172 | + return client.MatchingLabels(map[string]string{ |
| 173 | + clusterv1.ClusterLabelName: s.Cluster.Name, |
| 174 | + }) |
| 175 | +} |
| 176 | + |
| 177 | +// PatchObject persists the cluster configuration and status. |
| 178 | +func (s *ExternalInfraClusterScope) PatchObject() error { |
| 179 | + return nil |
| 180 | +} |
| 181 | + |
| 182 | +// Close closes the current scope persisting the cluster configuration and status. |
| 183 | +func (s *ExternalInfraClusterScope) Close() error { |
| 184 | + return s.PatchObject() |
| 185 | +} |
| 186 | + |
| 187 | +// AdditionalTags returns AdditionalTags from the scope's ExternalInfraCluster. The returned value will never be nil. |
| 188 | +func (s *ExternalInfraClusterScope) AdditionalTags() infrav1.Tags { |
| 189 | + return nil |
| 190 | +} |
| 191 | + |
| 192 | +// APIServerPort returns the APIServerPort to use when creating the load balancer. |
| 193 | +func (s *ExternalInfraClusterScope) APIServerPort() int32 { |
| 194 | + if s.Cluster.Spec.ClusterNetwork != nil && s.Cluster.Spec.ClusterNetwork.APIServerPort != nil { |
| 195 | + return *s.Cluster.Spec.ClusterNetwork.APIServerPort |
| 196 | + } |
| 197 | + return 6443 |
| 198 | +} |
| 199 | + |
| 200 | +// SetFailureDomain sets the infrastructure provider failure domain key to the spec given as input. |
| 201 | +func (s *ExternalInfraClusterScope) SetFailureDomain(id string, spec clusterv1.FailureDomainSpec) { |
| 202 | +} |
| 203 | + |
| 204 | +type ExternalInfraClusterObject struct { |
| 205 | + *unstructured.Unstructured |
| 206 | +} |
| 207 | + |
| 208 | +// InfraCluster returns the AWS infrastructure cluster or control plane object. |
| 209 | +func (s *ExternalInfraClusterScope) InfraCluster() cloud.ClusterObject { |
| 210 | + return s.ExternalInfraCluster |
| 211 | +} |
| 212 | + |
| 213 | +func (r *ExternalInfraClusterObject) GetConditions() clusterv1.Conditions { |
| 214 | + return nil |
| 215 | +} |
| 216 | + |
| 217 | +func (r *ExternalInfraClusterObject) SetConditions(conditions clusterv1.Conditions) { |
| 218 | +} |
| 219 | + |
| 220 | +// Session returns the AWS SDK session. Used for creating clients |
| 221 | +func (s *ExternalInfraClusterScope) Session() awsclient.ConfigProvider { |
| 222 | + return s.session |
| 223 | +} |
| 224 | + |
| 225 | +// Bastion returns the bastion details. |
| 226 | +func (s *ExternalInfraClusterScope) Bastion() *infrav1.Bastion { |
| 227 | + return nil |
| 228 | +} |
| 229 | + |
| 230 | +// SetBastionInstance sets the bastion instance in the status of the cluster. |
| 231 | +func (s *ExternalInfraClusterScope) SetBastionInstance(instance *infrav1.Instance) { |
| 232 | +} |
| 233 | + |
| 234 | +// SSHKeyName returns the SSH key name to use for instances. |
| 235 | +func (s *ExternalInfraClusterScope) SSHKeyName() *string { |
| 236 | + return nil |
| 237 | +} |
| 238 | + |
| 239 | +// ControllerName returns the name of the controller that |
| 240 | +// created the ExternalInfraClusterScope. |
| 241 | +func (s *ExternalInfraClusterScope) ControllerName() string { |
| 242 | + return s.controllerName |
| 243 | +} |
| 244 | + |
| 245 | +// ImageLookupFormat returns the format string to use when looking up AMIs |
| 246 | +func (s *ExternalInfraClusterScope) ImageLookupFormat() string { |
| 247 | + return "" |
| 248 | +} |
| 249 | + |
| 250 | +// ImageLookupOrg returns the organization name to use when looking up AMIs |
| 251 | +func (s *ExternalInfraClusterScope) ImageLookupOrg() string { |
| 252 | + return "" |
| 253 | +} |
| 254 | + |
| 255 | +// ImageLookupBaseOS returns the base operating system name to use when looking up AMIs |
| 256 | +func (s *ExternalInfraClusterScope) ImageLookupBaseOS() string { |
| 257 | + return "" |
| 258 | +} |
0 commit comments