Skip to content

Commit 38a2eba

Browse files
committed
capi 1.6.x: add copy of providerid
1 parent 241896e commit 38a2eba

26 files changed

+513
-1003
lines changed

.github/workflows/lint.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,4 @@ jobs:
1818
- name: golangci-lint
1919
uses: golangci/golangci-lint-action@v3
2020
with:
21-
version: "v1.53"
21+
version: "v1.55"

Makefile

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ ENVSUBST_VER := v1.4.2
7171
ENVSUBST_BIN := envsubst
7272
ENVSUBST := $(TOOLS_BIN_DIR)/$(ENVSUBST_BIN)
7373

74-
GOLANGCI_LINT_VER := v1.53.3
74+
GOLANGCI_LINT_VER := v1.55.2
7575
GOLANGCI_LINT_BIN := golangci-lint
7676
GOLANGCI_LINT := $(TOOLS_BIN_DIR)/$(GOLANGCI_LINT_BIN)-$(GOLANGCI_LINT_VER)
7777

@@ -320,7 +320,8 @@ generate: ## Generate code
320320
.PHONY: generate-go
321321
generate-go: $(CONTROLLER_GEN) $(CONVERSION_GEN) ## Runs Go related generate targets
322322
$(CONTROLLER_GEN) \
323-
paths=./api/... \
323+
paths=./ \
324+
paths=./... \
324325
paths=./$(EXP_DIR)/api/... \
325326
object:headerFile=./hack/boilerplate/boilerplate.generatego.txt
326327
$(CONVERSION_GEN) \
@@ -334,6 +335,7 @@ generate-go: $(CONTROLLER_GEN) $(CONVERSION_GEN) ## Runs Go related generate tar
334335
.PHONY: generate-manifests
335336
generate-manifests: $(CONTROLLER_GEN) ## Generate manifests e.g. CRD, RBAC etc.
336337
$(CONTROLLER_GEN) \
338+
paths=./ \
337339
paths=./api/... \
338340
paths=./$(EXP_DIR)/api/... \
339341
crd:crdVersions=v1 \
@@ -342,6 +344,7 @@ generate-manifests: $(CONTROLLER_GEN) ## Generate manifests e.g. CRD, RBAC etc.
342344
output:webhook:dir=$(WEBHOOK_ROOT) \
343345
webhook
344346
$(CONTROLLER_GEN) \
347+
paths=./ \
345348
paths=./controllers/... \
346349
paths=./$(EXP_DIR)/controllers/... \
347350
output:rbac:dir=$(RBAC_ROOT) \

api/v1alpha4/zz_generated.conversion.go

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

api/v1alpha4/zz_generated.deepcopy.go

Lines changed: 0 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

api/v1beta1/zz_generated.deepcopy.go

Lines changed: 0 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

cloud/scope/machine.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@ import (
3737
"sigs.k8s.io/cluster-api-provider-gcp/cloud/providerid"
3838
"sigs.k8s.io/cluster-api-provider-gcp/cloud/services/shared"
3939
clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1"
40-
"sigs.k8s.io/cluster-api/controllers/noderefutil"
4140
capierrors "sigs.k8s.io/cluster-api/errors"
4241
"sigs.k8s.io/cluster-api/util"
4342
"sigs.k8s.io/cluster-api/util/patch"
@@ -148,12 +147,12 @@ func (m *MachineScope) Role() string {
148147

149148
// GetInstanceID returns the GCPMachine instance id by parsing Spec.ProviderID.
150149
func (m *MachineScope) GetInstanceID() *string {
151-
parsed, err := noderefutil.NewProviderID(m.GetProviderID()) //nolint: staticcheck
150+
parsed, err := NewProviderID(m.GetProviderID())
152151
if err != nil {
153152
return nil
154153
}
155154

156-
return ptr.To[string](parsed.ID()) //nolint: staticcheck
155+
return ptr.To[string](parsed.ID())
157156
}
158157

159158
// GetProviderID returns the GCPMachine providerID from the spec.

cloud/scope/providerid.go

Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
/*
2+
Copyright 2024 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

Comments
 (0)