|
| 1 | +/* |
| 2 | +Copyright 2018 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 scope |
| 18 | + |
| 19 | +import ( |
| 20 | + "regexp" |
| 21 | + "strings" |
| 22 | + |
| 23 | + "github.com/pkg/errors" |
| 24 | +) |
| 25 | + |
| 26 | +// Copied from https://github.com/kubernetes-sigs/cluster-api/blob/bda002f52575eeaff68da1ba33c8ef27d5b1014c/controllers/noderefutil/providerid.go |
| 27 | +// As this is removed by https://github.com/kubernetes-sigs/cluster-api/pull/9136 |
| 28 | +var ( |
| 29 | + // ErrEmptyProviderID means that the provider id is empty. |
| 30 | + // |
| 31 | + // Deprecated: This var is going to be removed in a future release. |
| 32 | + ErrEmptyProviderID = errors.New("providerID is empty") |
| 33 | + |
| 34 | + // ErrInvalidProviderID means that the provider id has an invalid form. |
| 35 | + // |
| 36 | + // Deprecated: This var is going to be removed in a future release. |
| 37 | + ErrInvalidProviderID = errors.New("providerID must be of the form <cloudProvider>://<optional>/<segments>/<provider id>") |
| 38 | +) |
| 39 | + |
| 40 | +// ProviderID is a struct representation of a Kubernetes ProviderID. |
| 41 | +// Format: cloudProvider://optional/segments/etc/id |
| 42 | +type ProviderID struct { |
| 43 | + original string |
| 44 | + cloudProvider string |
| 45 | + id string |
| 46 | +} |
| 47 | + |
| 48 | +/* |
| 49 | +- must start with at least one non-colon |
| 50 | +- followed by :// |
| 51 | +- followed by any number of characters |
| 52 | +- must end with a non-slash. |
| 53 | +*/ |
| 54 | +var providerIDRegex = regexp.MustCompile("^[^:]+://.*[^/]$") |
| 55 | + |
| 56 | +// NewProviderID parses the input string and returns a new ProviderID. |
| 57 | +func NewProviderID(id string) (*ProviderID, error) { |
| 58 | + if id == "" { |
| 59 | + return nil, ErrEmptyProviderID |
| 60 | + } |
| 61 | + |
| 62 | + if !providerIDRegex.MatchString(id) { |
| 63 | + return nil, ErrInvalidProviderID |
| 64 | + } |
| 65 | + |
| 66 | + colonIndex := strings.Index(id, ":") |
| 67 | + cloudProvider := id[0:colonIndex] |
| 68 | + |
| 69 | + lastSlashIndex := strings.LastIndex(id, "/") |
| 70 | + instance := id[lastSlashIndex+1:] |
| 71 | + |
| 72 | + res := &ProviderID{ |
| 73 | + original: id, |
| 74 | + cloudProvider: cloudProvider, |
| 75 | + id: instance, |
| 76 | + } |
| 77 | + |
| 78 | + if !res.Validate() { |
| 79 | + return nil, ErrInvalidProviderID |
| 80 | + } |
| 81 | + |
| 82 | + return res, nil |
| 83 | +} |
| 84 | + |
| 85 | +// CloudProvider returns the cloud provider portion of the ProviderID. |
| 86 | +// |
| 87 | +// Deprecated: This method is going to be removed in a future release. |
| 88 | +func (p *ProviderID) CloudProvider() string { |
| 89 | + return p.cloudProvider |
| 90 | +} |
| 91 | + |
| 92 | +// ID returns the identifier portion of the ProviderID. |
| 93 | +// |
| 94 | +// Deprecated: This method is going to be removed in a future release. |
| 95 | +func (p *ProviderID) ID() string { |
| 96 | + return p.id |
| 97 | +} |
| 98 | + |
| 99 | +// Equals returns true if this ProviderID string matches another ProviderID string. |
| 100 | +// |
| 101 | +// Deprecated: This method is going to be removed in a future release. |
| 102 | +func (p *ProviderID) Equals(o *ProviderID) bool { |
| 103 | + return p.String() == o.String() |
| 104 | +} |
| 105 | + |
| 106 | +// String returns the string representation of this object. |
| 107 | +// |
| 108 | +// Deprecated: This method is going to be removed in a future release. |
| 109 | +func (p ProviderID) String() string { |
| 110 | + return p.original |
| 111 | +} |
| 112 | + |
| 113 | +// Validate returns true if the provider id is valid. |
| 114 | +// |
| 115 | +// Deprecated: This method is going to be removed in a future release. |
| 116 | +func (p *ProviderID) Validate() bool { |
| 117 | + return p.CloudProvider() != "" && p.ID() != "" |
| 118 | +} |
| 119 | + |
| 120 | +// IndexKey returns the required level of uniqueness |
| 121 | +// to represent and index machines uniquely from their node providerID. |
| 122 | +// |
| 123 | +// Deprecated: This method is going to be removed in a future release. |
| 124 | +func (p *ProviderID) IndexKey() string { |
| 125 | + return p.String() |
| 126 | +} |
0 commit comments