sigs.k8s.io/cluster-api@v1.7.1/cmd/clusterctl/client/client.go (about) 1 /* 2 Copyright 2019 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 client 18 19 import ( 20 "context" 21 22 clusterctlv1 "sigs.k8s.io/cluster-api/cmd/clusterctl/api/v1alpha3" 23 "sigs.k8s.io/cluster-api/cmd/clusterctl/client/alpha" 24 "sigs.k8s.io/cluster-api/cmd/clusterctl/client/cluster" 25 "sigs.k8s.io/cluster-api/cmd/clusterctl/client/config" 26 "sigs.k8s.io/cluster-api/cmd/clusterctl/client/repository" 27 "sigs.k8s.io/cluster-api/cmd/clusterctl/client/tree" 28 ) 29 30 // Client is exposes the clusterctl high-level client library. 31 type Client interface { 32 // GetProvidersConfig returns the list of providers configured for this instance of clusterctl. 33 GetProvidersConfig() ([]Provider, error) 34 35 // GetProviderComponents returns the provider components for a given provider with options including targetNamespace. 36 GetProviderComponents(ctx context.Context, provider string, providerType clusterctlv1.ProviderType, options ComponentsOptions) (Components, error) 37 38 // GenerateProvider returns the provider components for a given provider with options including targetNamespace. 39 GenerateProvider(ctx context.Context, provider string, providerType clusterctlv1.ProviderType, options ComponentsOptions) (Components, error) 40 41 // Init initializes a management cluster by adding the requested list of providers. 42 Init(ctx context.Context, options InitOptions) ([]Components, error) 43 44 // InitImages returns the list of images required for executing the init command. 45 InitImages(ctx context.Context, options InitOptions) ([]string, error) 46 47 // GetClusterTemplate returns a workload cluster template. 48 GetClusterTemplate(ctx context.Context, options GetClusterTemplateOptions) (Template, error) 49 50 // GetKubeconfig returns the kubeconfig of the workload cluster. 51 GetKubeconfig(ctx context.Context, options GetKubeconfigOptions) (string, error) 52 53 // Delete deletes providers from a management cluster. 54 Delete(ctx context.Context, options DeleteOptions) error 55 56 // Move moves all the Cluster API objects existing in a namespace (or from all the namespaces if empty) to a target management cluster. 57 Move(ctx context.Context, options MoveOptions) error 58 59 // PlanUpgrade returns a set of suggested Upgrade plans for the cluster. 60 PlanUpgrade(ctx context.Context, options PlanUpgradeOptions) ([]UpgradePlan, error) 61 62 // PlanCertManagerUpgrade returns a CertManagerUpgradePlan. 63 PlanCertManagerUpgrade(ctx context.Context, options PlanUpgradeOptions) (CertManagerUpgradePlan, error) 64 65 // ApplyUpgrade executes an upgrade plan. 66 ApplyUpgrade(ctx context.Context, options ApplyUpgradeOptions) error 67 68 // ProcessYAML provides a direct way to process a yaml and inspect its 69 // variables. 70 ProcessYAML(ctx context.Context, options ProcessYAMLOptions) (YamlPrinter, error) 71 72 // DescribeCluster returns the object tree representing the status of a Cluster API cluster. 73 DescribeCluster(ctx context.Context, options DescribeClusterOptions) (*tree.ObjectTree, error) 74 75 // AlphaClient is an Interface for alpha features in clusterctl 76 AlphaClient 77 } 78 79 // AlphaClient exposes the alpha features in clusterctl high-level client library. 80 type AlphaClient interface { 81 // RolloutRestart provides rollout restart of cluster-api resources 82 RolloutRestart(ctx context.Context, options RolloutRestartOptions) error 83 // RolloutPause provides rollout pause of cluster-api resources 84 RolloutPause(ctx context.Context, options RolloutPauseOptions) error 85 // RolloutResume provides rollout resume of paused cluster-api resources 86 RolloutResume(ctx context.Context, options RolloutResumeOptions) error 87 // RolloutUndo provides rollout rollback of cluster-api resources 88 RolloutUndo(ctx context.Context, options RolloutUndoOptions) error 89 // TopologyPlan dry runs the topology reconciler 90 // 91 // Deprecated: TopologyPlan is deprecated and will be removed in one of the upcoming releases. 92 TopologyPlan(ctx context.Context, options TopologyPlanOptions) (*TopologyPlanOutput, error) 93 } 94 95 // YamlPrinter exposes methods that prints the processed template and 96 // variables. 97 type YamlPrinter interface { 98 // Variables required by the template. 99 Variables() []string 100 101 // Yaml returns yaml defining all the cluster template objects as a byte array. 102 Yaml() ([]byte, error) 103 } 104 105 // clusterctlClient implements Client. 106 type clusterctlClient struct { 107 configClient config.Client 108 repositoryClientFactory RepositoryClientFactory 109 clusterClientFactory ClusterClientFactory 110 alphaClient alpha.Client 111 } 112 113 // RepositoryClientFactoryInput represents the inputs required by the factory. 114 type RepositoryClientFactoryInput struct { 115 Provider Provider 116 Processor Processor 117 } 118 119 // RepositoryClientFactory is a factory of repository.Client from a given input. 120 type RepositoryClientFactory func(context.Context, RepositoryClientFactoryInput) (repository.Client, error) 121 122 // ClusterClientFactoryInput represents the inputs required by the factory. 123 type ClusterClientFactoryInput struct { 124 Kubeconfig Kubeconfig 125 Processor Processor 126 } 127 128 // ClusterClientFactory is a factory of cluster.Client from a given input. 129 type ClusterClientFactory func(ClusterClientFactoryInput) (cluster.Client, error) 130 131 // Ensure clusterctlClient implements Client. 132 var _ Client = &clusterctlClient{} 133 134 // Option is a configuration option supplied to New. 135 type Option func(*clusterctlClient) 136 137 // InjectConfig allows to override the default configuration client used by clusterctl. 138 func InjectConfig(config config.Client) Option { 139 return func(c *clusterctlClient) { 140 c.configClient = config 141 } 142 } 143 144 // InjectRepositoryFactory allows to override the default factory used for creating 145 // RepositoryClient objects. 146 func InjectRepositoryFactory(factory RepositoryClientFactory) Option { 147 return func(c *clusterctlClient) { 148 c.repositoryClientFactory = factory 149 } 150 } 151 152 // InjectClusterClientFactory allows to override the default factory used for creating 153 // ClusterClient objects. 154 func InjectClusterClientFactory(factory ClusterClientFactory) Option { 155 return func(c *clusterctlClient) { 156 c.clusterClientFactory = factory 157 } 158 } 159 160 // New returns a configClient. 161 func New(ctx context.Context, path string, options ...Option) (Client, error) { 162 return newClusterctlClient(ctx, path, options...) 163 } 164 165 func newClusterctlClient(ctx context.Context, path string, options ...Option) (*clusterctlClient, error) { 166 client := &clusterctlClient{} 167 for _, o := range options { 168 o(client) 169 } 170 171 // if there is an injected config, use it, otherwise use the default one 172 // provided by the config low level library. 173 if client.configClient == nil { 174 c, err := config.New(ctx, path) 175 if err != nil { 176 return nil, err 177 } 178 client.configClient = c 179 } 180 181 // if there is an injected RepositoryFactory, use it, otherwise use a default one. 182 if client.repositoryClientFactory == nil { 183 client.repositoryClientFactory = defaultRepositoryFactory(client.configClient) 184 } 185 186 // if there is an injected ClusterFactory, use it, otherwise use a default one. 187 if client.clusterClientFactory == nil { 188 client.clusterClientFactory = defaultClusterFactory(client.configClient) 189 } 190 191 // if there is an injected alphaClient, use it, otherwise use a default one. 192 if client.alphaClient == nil { 193 c := alpha.New() 194 client.alphaClient = c 195 } 196 197 return client, nil 198 } 199 200 // defaultRepositoryFactory is a RepositoryClientFactory func the uses the default client provided by the repository low level library. 201 func defaultRepositoryFactory(configClient config.Client) RepositoryClientFactory { 202 return func(ctx context.Context, input RepositoryClientFactoryInput) (repository.Client, error) { 203 return repository.New( 204 ctx, 205 input.Provider, 206 configClient, 207 repository.InjectYamlProcessor(input.Processor), 208 ) 209 } 210 } 211 212 // defaultClusterFactory is a ClusterClientFactory func the uses the default client provided by the cluster low level library. 213 func defaultClusterFactory(configClient config.Client) ClusterClientFactory { 214 return func(input ClusterClientFactoryInput) (cluster.Client, error) { 215 return cluster.New( 216 // Kubeconfig is a type alias to cluster.Kubeconfig 217 cluster.Kubeconfig(input.Kubeconfig), 218 configClient, 219 cluster.InjectYamlProcessor(input.Processor), 220 ), nil 221 } 222 }