yunion.io/x/cloudmux@v0.3.10-0-alpha.1/pkg/multicloud/azure/kube_cluster.go (about) 1 // Copyright 2019 Yunion 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package azure 16 17 import ( 18 "encoding/base64" 19 "strings" 20 21 "yunion.io/x/jsonutils" 22 "yunion.io/x/pkg/errors" 23 24 "yunion.io/x/cloudmux/pkg/cloudprovider" 25 "yunion.io/x/cloudmux/pkg/multicloud" 26 ) 27 28 type SKubeCluster struct { 29 multicloud.SResourceBase 30 AzureTags 31 region *SRegion 32 33 Id string `json:"id"` 34 Location string `json:"location"` 35 Name string `json:"name"` 36 Type string `json:"type"` 37 Properties struct { 38 ProvisioningState string `json:"provisioningState"` 39 PowerState struct { 40 Code string `json:"code"` 41 } `json:"powerState"` 42 KubernetesVersion string `json:"kubernetesVersion"` 43 DNSPrefix string `json:"dnsPrefix"` 44 Fqdn string `json:"fqdn"` 45 AzurePortalFQDN string `json:"azurePortalFQDN"` 46 AgentPoolProfiles []SKubeNodePool `json:"agentPoolProfiles"` 47 ServicePrincipalProfile struct { 48 ClientId string `json:"clientId"` 49 } `json:"servicePrincipalProfile"` 50 AddonProfiles struct { 51 Azurepolicy struct { 52 Enabled bool `json:"enabled"` 53 Config string `json:"config"` 54 } `json:"azurepolicy"` 55 HTTPApplicationRouting struct { 56 Enabled bool `json:"enabled"` 57 Config struct { 58 HTTPApplicationRoutingZoneName string `json:"HTTPApplicationRoutingZoneName"` 59 } `json:"config"` 60 } `json:"httpApplicationRouting"` 61 OmsAgent struct { 62 Enabled bool `json:"enabled"` 63 Config struct { 64 LogAnalyticsWorkspaceResourceId string `json:"logAnalyticsWorkspaceResourceId"` 65 } `json:"config"` 66 Identity struct { 67 ResourceId string `json:"resourceId"` 68 ClientId string `json:"clientId"` 69 ObjectId string `json:"objectId"` 70 } `json:"identity"` 71 } `json:"omsAgent"` 72 } `json:"addonProfiles"` 73 NodeResourceGroup string `json:"nodeResourceGroup"` 74 EnableRBAC bool `json:"enableRBAC"` 75 NetworkProfile struct { 76 NetworkPlugin string `json:"networkPlugin"` 77 LoadBalancerSku string `json:"loadBalancerSku"` 78 LoadBalancerProfile struct { 79 ManagedOutboundIPs struct { 80 Count int `json:"count"` 81 } `json:"managedOutboundIPs"` 82 EffectiveOutboundIPs []struct { 83 Id string `json:"id"` 84 } `json:"effectiveOutboundIPs"` 85 } `json:"loadBalancerProfile"` 86 PodCidr string `json:"podCidr"` 87 ServiceCidr string `json:"serviceCidr"` 88 DNSServiceIP string `json:"dnsServiceIP"` 89 DockerBridgeCidr string `json:"dockerBridgeCidr"` 90 OutboundType string `json:"outboundType"` 91 } `json:"networkProfile"` 92 MaxAgentPools int `json:"maxAgentPools"` 93 APIServerAccessProfile struct { 94 EnablePrivateCluster bool `json:"enablePrivateCluster"` 95 } `json:"apiServerAccessProfile"` 96 IdentityProfile struct { 97 Kubeletidentity struct { 98 ResourceId string `json:"resourceId"` 99 ClientId string `json:"clientId"` 100 ObjectId string `json:"objectId"` 101 } `json:"kubeletidentity"` 102 } `json:"identityProfile"` 103 } `json:"properties"` 104 Identity struct { 105 Type string `json:"type"` 106 PrincipalId string `json:"principalId"` 107 TenantId string `json:"tenantId"` 108 } `json:"identity"` 109 Sku struct { 110 Name string `json:"name"` 111 Tier string `json:"tier"` 112 } `json:"sku"` 113 } 114 115 func (self *SKubeCluster) GetName() string { 116 return self.Name 117 } 118 119 func (self *SKubeCluster) GetId() string { 120 return self.Id 121 } 122 123 func (self *SKubeCluster) GetGlobalId() string { 124 return strings.ToLower(self.Id) 125 } 126 127 func (self *SKubeCluster) Refresh() error { 128 cluster, err := self.region.GetKubeCluster(self.Id) 129 if err != nil { 130 return err 131 } 132 return jsonutils.Update(self, cluster) 133 } 134 135 func (self *SKubeCluster) GetStatus() string { 136 return strings.ToLower(self.Properties.PowerState.Code) 137 } 138 139 func (self *SKubeCluster) Delete(isRetain bool) error { 140 return self.region.Delete(self.Id) 141 } 142 143 func (self *SKubeCluster) GetEnabled() bool { 144 return true 145 } 146 147 func (self *SKubeCluster) GetIKubeNodes() ([]cloudprovider.ICloudKubeNode, error) { 148 return []cloudprovider.ICloudKubeNode{}, nil 149 } 150 151 func (self *SKubeCluster) GetIKubeNodePools() ([]cloudprovider.ICloudKubeNodePool, error) { 152 ret := []cloudprovider.ICloudKubeNodePool{} 153 for i := range self.Properties.AgentPoolProfiles { 154 self.Properties.AgentPoolProfiles[i].cluster = self 155 ret = append(ret, &self.Properties.AgentPoolProfiles[i]) 156 } 157 return ret, nil 158 } 159 160 func (self *SKubeCluster) GetKubeConfig(private bool, expireMinute int) (*cloudprovider.SKubeconfig, error) { 161 return self.region.GetKubeConfig(self.Id) 162 } 163 164 func (self *SRegion) GetICloudKubeClusters() ([]cloudprovider.ICloudKubeCluster, error) { 165 clusters, err := self.GetKubeClusters() 166 if err != nil { 167 return nil, errors.Wrapf(err, "GetKubClusters") 168 } 169 ret := []cloudprovider.ICloudKubeCluster{} 170 for i := range clusters { 171 clusters[i].region = self 172 ret = append(ret, &clusters[i]) 173 } 174 return ret, nil 175 } 176 177 func (self *SRegion) GetICloudKubeClusterById(id string) (cloudprovider.ICloudKubeCluster, error) { 178 cluster, err := self.GetKubeCluster(id) 179 if err != nil { 180 return nil, errors.Wrapf(err, "GetKubeCluster(%s)", id) 181 } 182 return cluster, nil 183 } 184 185 func (self *SRegion) GetKubeCluster(id string) (*SKubeCluster, error) { 186 ret := &SKubeCluster{region: self} 187 return ret, self.get(id, nil, ret) 188 } 189 190 func (self *SRegion) GetKubeClusters() ([]SKubeCluster, error) { 191 clusters := []SKubeCluster{} 192 return clusters, self.list("Microsoft.ContainerService/managedClusters", nil, &clusters) 193 } 194 195 func (self *SRegion) GetKubeConfig(id string) (*cloudprovider.SKubeconfig, error) { 196 resp, err := self.perform(id, "listClusterAdminCredential", nil) 197 if err != nil { 198 return nil, errors.Wrapf(err, "listClusterAdminCredential") 199 } 200 ret := struct { 201 Kubeconfigs []struct { 202 Name string 203 Value string 204 } 205 }{} 206 err = resp.Unmarshal(&ret) 207 if err != nil { 208 return nil, errors.Wrapf(err, "resp.Unmarshal") 209 } 210 if len(ret.Kubeconfigs) == 0 { 211 return nil, errors.Wrapf(cloudprovider.ErrNotFound, "empty kubeconfig") 212 } 213 result := &cloudprovider.SKubeconfig{} 214 config, err := base64.StdEncoding.DecodeString(ret.Kubeconfigs[0].Value) 215 if err != nil { 216 return nil, errors.Wrapf(err, "base64.decode") 217 } 218 result.Config = string(config) 219 return result, err 220 }