forked from gardener/gardener
/
alicloudbotanist.go
119 lines (101 loc) · 4.34 KB
/
alicloudbotanist.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
// Copyright (c) 2018 SAP SE or an SAP affiliate company. All rights reserved. This file is licensed under the Apache Software License, v. 2 except as noted otherwise in the LICENSE file
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package alicloudbotanist
import (
"errors"
"fmt"
gardenv1beta1 "github.com/gardener/gardener/pkg/apis/garden/v1beta1"
"github.com/gardener/gardener/pkg/operation"
"github.com/gardener/gardener/pkg/operation/common"
"github.com/gardener/gardener-extensions/controllers/provider-alicloud/pkg/apis/alicloud"
alicloudv1alpha1 "github.com/gardener/gardener-extensions/controllers/provider-alicloud/pkg/apis/alicloud/v1alpha1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/serializer"
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
)
// IMPORTANT NOTICE
// The following part is only temporarily needed until we have completed the Extensibility epic
// and moved out all provider specifics.
// IMPORTANT NOTICE
var (
scheme *runtime.Scheme
decoder runtime.Decoder
)
func init() {
scheme = runtime.NewScheme()
// Workaround for incompatible kubernetes dependencies in gardener/gardener and
// gardener/gardener-extensions.
alicloudSchemeBuilder := runtime.NewSchemeBuilder(func(scheme *runtime.Scheme) error {
scheme.AddKnownTypes(alicloud.SchemeGroupVersion, &alicloud.InfrastructureConfig{}, &alicloud.InfrastructureStatus{})
return nil
})
alicloudv1alpha1SchemeBuilder := runtime.NewSchemeBuilder(func(scheme *runtime.Scheme) error {
scheme.AddKnownTypes(alicloudv1alpha1.SchemeGroupVersion, &alicloudv1alpha1.InfrastructureConfig{}, &alicloudv1alpha1.InfrastructureStatus{})
return nil
})
schemeBuilder := runtime.NewSchemeBuilder(
alicloudv1alpha1SchemeBuilder.AddToScheme,
alicloudSchemeBuilder.AddToScheme,
)
utilruntime.Must(schemeBuilder.AddToScheme(scheme))
decoder = serializer.NewCodecFactory(scheme).UniversalDecoder()
}
func infrastructureStatusFromInfrastructure(raw []byte) (*alicloudv1alpha1.InfrastructureStatus, error) {
config := &alicloudv1alpha1.InfrastructureStatus{}
if _, _, err := decoder.Decode(raw, nil, config); err != nil {
return nil, err
}
return config, nil
}
func findVSWitchByPurposeAndZone(vswitches []alicloudv1alpha1.VSwitch, purpose alicloudv1alpha1.Purpose, zone string) (*alicloudv1alpha1.VSwitch, error) {
for _, vswitch := range vswitches {
if vswitch.Purpose == purpose && vswitch.Zone == zone {
return &vswitch, nil
}
}
return nil, fmt.Errorf("no vswitch with purpose %q in zone %q found", purpose, zone)
}
func findSecurityGroupByPurpose(securityGroups []alicloudv1alpha1.SecurityGroup, purpose alicloudv1alpha1.Purpose) (*alicloudv1alpha1.SecurityGroup, error) {
for _, securityGroup := range securityGroups {
if securityGroup.Purpose == purpose {
return &securityGroup, nil
}
}
return nil, fmt.Errorf("cannot find security group with purpose %q", purpose)
}
// IMPORTANT NOTICE
// The above part is only temporarily needed until we have completed the Extensibility epic
// and moved out all provider specifics.
// IMPORTANT NOTICE
// New takes an operation object <o> and creates a new AlicloudBotanist object.
func New(o *operation.Operation, purpose string) (*AlicloudBotanist, error) {
var cloudProvider gardenv1beta1.CloudProvider
switch purpose {
case common.CloudPurposeShoot:
cloudProvider = o.Shoot.CloudProvider
case common.CloudPurposeSeed:
cloudProvider = o.Seed.CloudProvider
}
if cloudProvider != gardenv1beta1.CloudProviderAlicloud {
return nil, errors.New("cannot instantiate an Alicloud botanist if neither Shoot nor Seed cluster specifies Alicloud")
}
return &AlicloudBotanist{
Operation: o,
CloudProviderName: "alicloud",
}, nil
}
// GetCloudProviderName returns the Kubernetes cloud provider name for this cloud.
func (b *AlicloudBotanist) GetCloudProviderName() string {
return b.CloudProviderName
}