sigs.k8s.io/cluster-api@v1.7.1/cmd/clusterctl/client/config/provider.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 config 18 19 import ( 20 "encoding/json" 21 "path/filepath" 22 23 clusterctlv1 "sigs.k8s.io/cluster-api/cmd/clusterctl/api/v1alpha3" 24 ) 25 26 // Provider defines a provider configuration. 27 type Provider interface { 28 // Name returns the name of the provider. 29 Name() string 30 31 // Type returns the type of the provider. 32 Type() clusterctlv1.ProviderType 33 34 // URL returns the name of the provider repository. 35 URL() string 36 37 // SameAs returns true if two providers have the same name and type. 38 // Please note that this uniquely identifies a provider configuration, but not the provider instances in the cluster 39 // because it is possible to create many instances of the same provider. 40 SameAs(other Provider) bool 41 42 // ManifestLabel returns the cluster.x-k8s.io/provider label value for a provider. 43 // Please note that this label uniquely identifies the provider, e.g. bootstrap-kubeadm, but not the instances of 44 // the provider, e.g. namespace-1/bootstrap-kubeadm and namespace-2/bootstrap-kubeadm 45 ManifestLabel() string 46 47 // Less func can be used to ensure a consist order of provider lists. 48 Less(other Provider) bool 49 } 50 51 // provider implements Provider. 52 type provider struct { 53 name string 54 url string 55 providerType clusterctlv1.ProviderType 56 } 57 58 // ensure provider implements provider. 59 var _ Provider = &provider{} 60 61 func (p *provider) Name() string { 62 return p.name 63 } 64 65 func (p *provider) URL() string { 66 return p.url 67 } 68 69 func (p *provider) Type() clusterctlv1.ProviderType { 70 return p.providerType 71 } 72 73 func (p *provider) SameAs(other Provider) bool { 74 return p.name == other.Name() && p.providerType == other.Type() 75 } 76 77 func (p *provider) ManifestLabel() string { 78 return clusterctlv1.ManifestLabel(p.name, p.Type()) 79 } 80 81 func (p *provider) Less(other Provider) bool { 82 return p.providerType.Order() < other.Type().Order() || 83 (p.providerType.Order() == other.Type().Order() && p.name < other.Name()) 84 } 85 86 // NewProvider creates a new Provider with the given input. 87 func NewProvider(name string, url string, ttype clusterctlv1.ProviderType) Provider { 88 return &provider{ 89 name: name, 90 url: url, 91 providerType: ttype, 92 } 93 } 94 95 func (p provider) MarshalJSON() ([]byte, error) { 96 dir, file := filepath.Split(p.url) 97 j, err := json.Marshal(struct { 98 Name string 99 ProviderType clusterctlv1.ProviderType 100 URL string 101 File string 102 }{ 103 Name: p.name, 104 ProviderType: p.providerType, 105 URL: dir, 106 File: file, 107 }) 108 if err != nil { 109 return nil, err 110 } 111 return j, nil 112 }