Skip to content

Commit

Permalink
capi 1.6.x: add copy of providerid
Browse files Browse the repository at this point in the history
  • Loading branch information
damdo committed Jan 31, 2024
1 parent 241896e commit d067593
Show file tree
Hide file tree
Showing 26 changed files with 513 additions and 1,004 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/lint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,4 @@ jobs:
- name: golangci-lint
uses: golangci/golangci-lint-action@v3
with:
version: "v1.53"
version: "v1.55"
7 changes: 5 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ ENVSUBST_VER := v1.4.2
ENVSUBST_BIN := envsubst
ENVSUBST := $(TOOLS_BIN_DIR)/$(ENVSUBST_BIN)

GOLANGCI_LINT_VER := v1.53.3
GOLANGCI_LINT_VER := v1.55.2
GOLANGCI_LINT_BIN := golangci-lint
GOLANGCI_LINT := $(TOOLS_BIN_DIR)/$(GOLANGCI_LINT_BIN)-$(GOLANGCI_LINT_VER)

Expand Down Expand Up @@ -320,7 +320,8 @@ generate: ## Generate code
.PHONY: generate-go
generate-go: $(CONTROLLER_GEN) $(CONVERSION_GEN) ## Runs Go related generate targets
$(CONTROLLER_GEN) \
paths=./api/... \
paths=./ \
paths=./... \
paths=./$(EXP_DIR)/api/... \
object:headerFile=./hack/boilerplate/boilerplate.generatego.txt
$(CONVERSION_GEN) \
Expand All @@ -334,6 +335,7 @@ generate-go: $(CONTROLLER_GEN) $(CONVERSION_GEN) ## Runs Go related generate tar
.PHONY: generate-manifests
generate-manifests: $(CONTROLLER_GEN) ## Generate manifests e.g. CRD, RBAC etc.
$(CONTROLLER_GEN) \
paths=./ \
paths=./api/... \
paths=./$(EXP_DIR)/api/... \
crd:crdVersions=v1 \
Expand All @@ -342,6 +344,7 @@ generate-manifests: $(CONTROLLER_GEN) ## Generate manifests e.g. CRD, RBAC etc.
output:webhook:dir=$(WEBHOOK_ROOT) \
webhook
$(CONTROLLER_GEN) \
paths=./ \
paths=./controllers/... \
paths=./$(EXP_DIR)/controllers/... \
output:rbac:dir=$(RBAC_ROOT) \
Expand Down
2 changes: 1 addition & 1 deletion api/v1alpha4/zz_generated.conversion.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion api/v1alpha4/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion api/v1beta1/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 2 additions & 3 deletions cloud/scope/machine.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ import (
"sigs.k8s.io/cluster-api-provider-gcp/cloud/providerid"
"sigs.k8s.io/cluster-api-provider-gcp/cloud/services/shared"
clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1"
"sigs.k8s.io/cluster-api/controllers/noderefutil"
capierrors "sigs.k8s.io/cluster-api/errors"
"sigs.k8s.io/cluster-api/util"
"sigs.k8s.io/cluster-api/util/patch"
Expand Down Expand Up @@ -148,12 +147,12 @@ func (m *MachineScope) Role() string {

// GetInstanceID returns the GCPMachine instance id by parsing Spec.ProviderID.
func (m *MachineScope) GetInstanceID() *string {
parsed, err := noderefutil.NewProviderID(m.GetProviderID()) //nolint: staticcheck
parsed, err := NewProviderID(m.GetProviderID())
if err != nil {
return nil
}

return ptr.To[string](parsed.ID()) //nolint: staticcheck
return ptr.To[string](parsed.ID())
}

// GetProviderID returns the GCPMachine providerID from the spec.
Expand Down
126 changes: 126 additions & 0 deletions cloud/scope/providerid.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
/*
Copyright 2024 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package scope

import (
"regexp"
"strings"

"github.com/pkg/errors"
)

// Copied from https://github.com/kubernetes-sigs/cluster-api/blob/bda002f52575eeaff68da1ba33c8ef27d5b1014c/controllers/noderefutil/providerid.go
// As this is removed by https://github.com/kubernetes-sigs/cluster-api/pull/9136
var (
// ErrEmptyProviderID means that the provider id is empty.
//
// Deprecated: This var is going to be removed in a future release.
ErrEmptyProviderID = errors.New("providerID is empty")

// ErrInvalidProviderID means that the provider id has an invalid form.
//
// Deprecated: This var is going to be removed in a future release.
ErrInvalidProviderID = errors.New("providerID must be of the form <cloudProvider>://<optional>/<segments>/<provider id>")
)

// ProviderID is a struct representation of a Kubernetes ProviderID.
// Format: cloudProvider://optional/segments/etc/id
type ProviderID struct {
original string
cloudProvider string
id string
}

/*
- must start with at least one non-colon
- followed by ://
- followed by any number of characters
- must end with a non-slash.
*/
var providerIDRegex = regexp.MustCompile("^[^:]+://.*[^/]$")

// NewProviderID parses the input string and returns a new ProviderID.
func NewProviderID(id string) (*ProviderID, error) {
if id == "" {
return nil, ErrEmptyProviderID
}

if !providerIDRegex.MatchString(id) {
return nil, ErrInvalidProviderID
}

colonIndex := strings.Index(id, ":")
cloudProvider := id[0:colonIndex]

lastSlashIndex := strings.LastIndex(id, "/")
instance := id[lastSlashIndex+1:]

res := &ProviderID{
original: id,
cloudProvider: cloudProvider,
id: instance,
}

if !res.Validate() {
return nil, ErrInvalidProviderID
}

return res, nil
}

// CloudProvider returns the cloud provider portion of the ProviderID.
//
// Deprecated: This method is going to be removed in a future release.
func (p *ProviderID) CloudProvider() string {
return p.cloudProvider
}

// ID returns the identifier portion of the ProviderID.
//
// Deprecated: This method is going to be removed in a future release.
func (p *ProviderID) ID() string {
return p.id
}

// Equals returns true if this ProviderID string matches another ProviderID string.
//
// Deprecated: This method is going to be removed in a future release.
func (p *ProviderID) Equals(o *ProviderID) bool {
return p.String() == o.String()
}

// String returns the string representation of this object.
//
// Deprecated: This method is going to be removed in a future release.
func (p ProviderID) String() string {
return p.original
}

// Validate returns true if the provider id is valid.
//
// Deprecated: This method is going to be removed in a future release.
func (p *ProviderID) Validate() bool {
return p.CloudProvider() != "" && p.ID() != ""
}

// IndexKey returns the required level of uniqueness
// to represent and index machines uniquely from their node providerID.
//
// Deprecated: This method is going to be removed in a future release.
func (p *ProviderID) IndexKey() string {
return p.String()
}
Loading

0 comments on commit d067593

Please sign in to comment.