This repository has been archived by the owner on Oct 10, 2023. It is now read-only.
/
docker.go
130 lines (116 loc) · 5.88 KB
/
docker.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
120
121
122
123
124
125
126
127
128
129
130
// Copyright 2021 VMware, Inc. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
package tkgconfigproviders
import (
"encoding/base64"
"strconv"
"github.com/vmware-tanzu/tanzu-framework/tkg/constants"
"github.com/vmware-tanzu/tanzu-framework/tkg/web/server/models"
)
// DockerConfig is the tkg config file for docker provider
type DockerConfig struct {
ClusterName string `yaml:"CLUSTER_NAME,omitempty"`
ClusterLabels string `yaml:"CLUSTER_LABELS,omitempty"`
ClusterAnnotations string `yaml:"CLUSTER_ANNOTATIONS,omitempty"`
InfrastructureProvider string `yaml:"INFRASTRUCTURE_PROVIDER,omitempty"`
ClusterPlan string `yaml:"CLUSTER_PLAN,omitempty"`
CeipParticipation string `yaml:"ENABLE_CEIP_PARTICIPATION,omitempty"`
MachineHealthCheckEnabled string `yaml:"ENABLE_MHC,omitempty"`
ServiceCIDR string `yaml:"SERVICE_CIDR,omitempty"`
ClusterCIDR string `yaml:"CLUSTER_CIDR,omitempty"`
ClusterHTTPProxy string `yaml:"TKG_HTTP_PROXY,omitempty"`
ClusterHTTPSProxy string `yaml:"TKG_HTTPS_PROXY,omitempty"`
ClusterNoProxy string `yaml:"TKG_NO_PROXY,omitempty"`
HTTPProxyEnabled string `yaml:"TKG_HTTP_PROXY_ENABLED"`
IDPConfig `yaml:",inline"`
OsInfo `yaml:",inline"`
}
func (c *client) NewDockerConfig(params *models.DockerRegionalClusterParams) (*DockerConfig, error) {
var err error
res := &DockerConfig{
ClusterName: params.ClusterName,
ClusterLabels: mapToConfigString(params.Labels),
ClusterAnnotations: mapToConfigString(params.Annotations),
InfrastructureProvider: constants.InfrastructureProviderDocker,
ClusterPlan: constants.PlanDev,
ClusterCIDR: params.Networking.ClusterPodCIDR,
ServiceCIDR: params.Networking.ClusterServiceCIDR,
HTTPProxyEnabled: falseConst,
}
if params.CeipOptIn != nil {
res.CeipParticipation = strconv.FormatBool(*params.CeipOptIn)
}
if params.IdentityManagement != nil { //nolint:dupl
res.IdentityManagementType = *params.IdentityManagement.IdmType
res.OIDCProviderName = params.IdentityManagement.OidcProviderName
res.OIDCIssuerURL = params.IdentityManagement.OidcProviderURL.String()
res.OIDCClientID = params.IdentityManagement.OidcClientID
res.OIDCClientSecret = params.IdentityManagement.OidcClientSecret
res.OIDCScopes = params.IdentityManagement.OidcScope
res.OIDCGroupsClaim = params.IdentityManagement.OidcClaimMappings["groups"]
res.OIDCUsernameClaim = params.IdentityManagement.OidcClaimMappings["username"]
res.LDAPBindDN = params.IdentityManagement.LdapBindDn
res.LDAPBindPassword = params.IdentityManagement.LdapBindPassword
res.LDAPHost = params.IdentityManagement.LdapURL
res.LDAPUserSearchBaseDN = params.IdentityManagement.LdapUserSearchBaseDn
res.LDAPUserSearchFilter = params.IdentityManagement.LdapUserSearchFilter
res.LDAPUserSearchUsername = params.IdentityManagement.LdapUserSearchUsername
res.LDAPUserSearchNameAttr = params.IdentityManagement.LdapUserSearchNameAttr
res.LDAPGroupSearchBaseDN = params.IdentityManagement.LdapGroupSearchBaseDn
res.LDAPGroupSearchFilter = params.IdentityManagement.LdapGroupSearchFilter
res.LDAPGroupSearchUserAttr = params.IdentityManagement.LdapGroupSearchUserAttr
res.LDAPGroupSearchGroupAttr = params.IdentityManagement.LdapGroupSearchGroupAttr
res.LDAPGroupSearchNameAttr = params.IdentityManagement.LdapGroupSearchNameAttr
res.LDAPRootCAData = base64.StdEncoding.EncodeToString([]byte(params.IdentityManagement.LdapRootCa))
}
if params.Networking != nil && params.Networking.HTTPProxyConfiguration != nil && params.Networking.HTTPProxyConfiguration.Enabled {
res.HTTPProxyEnabled = trueConst
conf := params.Networking.HTTPProxyConfiguration
res.ClusterHTTPProxy, err = CheckAndGetProxyURL(conf.HTTPProxyUsername, conf.HTTPProxyPassword, conf.HTTPProxyURL)
if err != nil {
return res, err
}
res.ClusterHTTPSProxy, err = CheckAndGetProxyURL(conf.HTTPSProxyUsername, conf.HTTPSProxyPassword, conf.HTTPSProxyURL)
if err != nil {
return res, err
}
res.ClusterNoProxy = params.Networking.HTTPProxyConfiguration.NoProxy
}
if params.MachineHealthCheckEnabled {
res.MachineHealthCheckEnabled = trueConst
} else {
res.MachineHealthCheckEnabled = falseConst
}
return res, nil
}
// CreateDockerParams generates a Params object from a DockerConfig, used for importing configuration files
func (c *client) CreateDockerParams(dockerConfig *DockerConfig) (params *models.DockerRegionalClusterParams, err error) {
ceipOptIn := dockerConfig.CeipParticipation == trueConst
params = &models.DockerRegionalClusterParams{
Annotations: configStringToMap(dockerConfig.ClusterAnnotations),
ClusterName: dockerConfig.ClusterName,
Networking: createDockerNetworkingConfig(dockerConfig),
CeipOptIn: &ceipOptIn,
ControlPlaneFlavor: "",
IdentityManagement: createIdentityManagementConfig(dockerConfig),
KubernetesVersion: "",
Labels: configStringToMap(dockerConfig.ClusterLabels),
MachineHealthCheckEnabled: dockerConfig.MachineHealthCheckEnabled == trueConst,
NumOfWorkerNodes: "",
}
return params, nil
}
// createDockerNetworkingConfig() creates a TKGNetwork from a docker config. Note that we need a special method here,
// because the other providers have a Networking object that they use within their xxxConfig object,
// but Docker just has the fields at the DockerConfig level
func createDockerNetworkingConfig(conf *DockerConfig) *models.TKGNetwork {
return &models.TKGNetwork{
ClusterDNSName: "",
ClusterNodeCIDR: "",
ClusterPodCIDR: conf.ClusterCIDR,
ClusterServiceCIDR: conf.ServiceCIDR,
CniType: "",
HTTPProxyConfiguration: createHTTPProxyConfig(conf),
NetworkName: "",
}
}