Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: adding e2e tests for gke (GKE Part 6) #844

Merged
merged 1 commit into from
Mar 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 2 additions & 5 deletions cloud/defaults.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
/*
Copyright 2021 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.
Expand All @@ -17,6 +14,6 @@ limitations under the License.
package cloud

const (
// ProviderIDPrefix is the gce provider id prefix.
ProviderIDPrefix = "gce://"
// DefaultNumRegionsPerZone is the default number of zones per region.
DefaultNumRegionsPerZone = 3
)
18 changes: 18 additions & 0 deletions cloud/providerid/doc.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/*
Copyright 2023 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 providerid implements functionality for creating kubernetes provider ids for nodes.
package providerid
89 changes: 89 additions & 0 deletions cloud/providerid/providerid.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
/*
Copyright 2023 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 providerid

import (
"errors"
"fmt"
"path"

"sigs.k8s.io/cluster-api-provider-gcp/util/resourceurl"
)

const (
// Prefix is the gce provider id prefix.
Prefix = "gce://"
)

// ProviderID represents the id for a GCP cluster.
type ProviderID interface {
Project() string
Location() string
Name() string
fmt.Stringer
}

// NewFromResourceURL creates a provider from a GCP resource url.
func NewFromResourceURL(url string) (ProviderID, error) {
resourceURL, err := resourceurl.Parse(url)
if err != nil {
return nil, fmt.Errorf("parsing resource url %s: %w", url, err)
}

return New(resourceURL.Project, resourceURL.Location, resourceURL.Name)
}

// New creates a new provider id.
func New(project, location, name string) (ProviderID, error) {
if project == "" {
return nil, errors.New("project required for provider id")
}
if location == "" {
return nil, errors.New("location required for provider id")
}
if name == "" {
return nil, errors.New("name required for provider id")
}

return &providerID{
project: project,
location: location,
name: name,
}, nil
}

type providerID struct {
project string
location string
name string
}

func (p *providerID) Project() string {
return p.project
}

func (p *providerID) Location() string {
return p.location
}

func (p *providerID) Name() string {
return p.name
}

func (p *providerID) String() string {
return Prefix + path.Join(p.project, p.location, p.name)
}
116 changes: 116 additions & 0 deletions cloud/providerid/providerid_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
/*
Copyright 2023 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 providerid_test

import (
"testing"

. "github.com/onsi/gomega"
"sigs.k8s.io/cluster-api-provider-gcp/cloud/providerid"
)

func TestProviderID_New(t *testing.T) {
RegisterTestingT(t)

testCases := []struct {
testname string
project string
location string
name string
expectedProviderID string
expectError bool
}{
{
testname: "no project, should fail",
project: "",
location: "eu-west4",
name: "vm1",
expectError: true,
},
{
testname: "no location, should fail",
project: "proj1",
location: "",
name: "vm1",
expectError: true,
},
{
testname: "no name, should fail",
project: "proj1",
location: "eu-west4",
name: "",
expectError: true,
},
{
testname: "with all details, should pass",
project: "proj1",
location: "eu-west4",
name: "vm1",
expectError: false,
expectedProviderID: "gce://proj1/eu-west4/vm1",
},
}

for _, tc := range testCases {
t.Run(tc.testname, func(t *testing.T) {
providerID, err := providerid.New(tc.project, tc.location, tc.name)

if tc.expectError {
Expect(err).To(HaveOccurred())
} else {
Expect(err).NotTo(HaveOccurred())
Expect(providerID.String()).To(Equal(tc.expectedProviderID))
}
})
}
}

func TestProviderID_NewFromResourceURL(t *testing.T) {
RegisterTestingT(t)

testCases := []struct {
testname string
resourceURL string
expectedProviderID string
expectError bool
}{
{
testname: "invalid url, should fail",
resourceURL: "hvfnhdkdk",
expectError: true,
},
{
testname: "valid instance url, should pass",
resourceURL: "https://www.googleapis.com/compute/v1/projects/myproject/zones/europe-west2-a/instances/gke-capg-dskczmdculd-capg-e2e-ebs0oy--014f89ba-sx2p",
expectError: false,
expectedProviderID: "gce://myproject/europe-west2-a/gke-capg-dskczmdculd-capg-e2e-ebs0oy--014f89ba-sx2p",
},
}

for _, tc := range testCases {
t.Run(tc.testname, func(t *testing.T) {
providerID, err := providerid.NewFromResourceURL(tc.resourceURL)

if tc.expectError {
Expect(err).To(HaveOccurred())
} else {
Expect(err).NotTo(HaveOccurred())
Expect(providerID.String()).To(Equal(tc.expectedProviderID))
}
})
}
}
5 changes: 3 additions & 2 deletions cloud/scope/machine.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import (
"k8s.io/utils/pointer"
infrav1 "sigs.k8s.io/cluster-api-provider-gcp/api/v1beta1"
"sigs.k8s.io/cluster-api-provider-gcp/cloud"
"sigs.k8s.io/cluster-api-provider-gcp/cloud/providerid"
clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1"
"sigs.k8s.io/cluster-api/controllers/noderefutil"
capierrors "sigs.k8s.io/cluster-api/errors"
Expand Down Expand Up @@ -169,8 +170,8 @@ func (m *MachineScope) GetProviderID() string {

// SetProviderID sets the GCPMachine providerID in spec.
func (m *MachineScope) SetProviderID() {
providerID := cloud.ProviderIDPrefix + path.Join(m.ClusterGetter.Project(), m.Zone(), m.Name())
m.GCPMachine.Spec.ProviderID = pointer.StringPtr(providerID)
providerID, _ := providerid.New(m.ClusterGetter.Project(), m.Zone(), m.Name())
m.GCPMachine.Spec.ProviderID = pointer.StringPtr(providerID.String())
}

// GetInstanceStatus returns the GCPMachine instance status.
Expand Down
5 changes: 5 additions & 0 deletions cloud/scope/managedcontrolplane.go
Original file line number Diff line number Diff line change
Expand Up @@ -215,3 +215,8 @@ func (s *ManagedControlPlaneScope) SetEndpoint(host string) {
Port: APIServerPort,
}
}

// IsAutopilotCluster returns true if this is an autopilot cluster.
func (s *ManagedControlPlaneScope) IsAutopilotCluster() bool {
return s.GCPManagedControlPlane.Spec.EnableAutopilot
}
13 changes: 9 additions & 4 deletions cloud/scope/managedmachinepool.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"context"
"fmt"

"sigs.k8s.io/cluster-api-provider-gcp/cloud"
"sigs.k8s.io/cluster-api-provider-gcp/util/location"

"sigs.k8s.io/cluster-api/util/conditions"
Expand Down Expand Up @@ -153,14 +154,18 @@ func (s *ManagedMachinePoolScope) NodePoolVersion() *string {
}

// ConvertToSdkNodePool converts a node pool to format that is used by GCP SDK.
func ConvertToSdkNodePool(nodePool infrav1exp.GCPManagedMachinePool, machinePool clusterv1exp.MachinePool) *containerpb.NodePool {
func ConvertToSdkNodePool(nodePool infrav1exp.GCPManagedMachinePool, machinePool clusterv1exp.MachinePool, regional bool) *containerpb.NodePool {
replicas := *machinePool.Spec.Replicas
if regional {
replicas /= cloud.DefaultNumRegionsPerZone
}
nodePoolName := nodePool.Spec.NodePoolName
if len(nodePoolName) == 0 {
nodePoolName = nodePool.Name
}
sdkNodePool := containerpb.NodePool{
Name: nodePoolName,
InitialNodeCount: nodePool.Spec.InitialNodeCount,
InitialNodeCount: replicas,
Config: &containerpb.NodeConfig{
Labels: nodePool.Spec.KubernetesLabels,
Taints: infrav1exp.ConvertToSdkTaint(nodePool.Spec.KubernetesTaints),
Expand All @@ -181,10 +186,10 @@ func ConvertToSdkNodePool(nodePool infrav1exp.GCPManagedMachinePool, machinePool
}

// ConvertToSdkNodePools converts node pools to format that is used by GCP SDK.
func ConvertToSdkNodePools(nodePools []infrav1exp.GCPManagedMachinePool, machinePools []clusterv1exp.MachinePool) []*containerpb.NodePool {
func ConvertToSdkNodePools(nodePools []infrav1exp.GCPManagedMachinePool, machinePools []clusterv1exp.MachinePool, regional bool) []*containerpb.NodePool {
res := []*containerpb.NodePool{}
for i := range nodePools {
res = append(res, ConvertToSdkNodePool(nodePools[i], machinePools[i]))
res = append(res, ConvertToSdkNodePool(nodePools[i], machinePools[i], regional))
}
return res
}
Expand Down
41 changes: 41 additions & 0 deletions cloud/services/container/clusters/errors.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*
Copyright 2023 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 clusters

import (
"fmt"

"github.com/pkg/errors"
)

var (
// ErrAutopilotClusterMachinePoolsNotAllowed is used when there are machine pools specified for an autopilot enabled cluster.
ErrAutopilotClusterMachinePoolsNotAllowed = errors.New("cannot use machine pools with an autopilot enabled cluster")
)

// NewErrUnexpectedClusterStatus creates a new error for an unexpected cluster status.
func NewErrUnexpectedClusterStatus(status string) error {
return &errUnexpectedClusterStatus{status}
}

type errUnexpectedClusterStatus struct {
status string
}

func (e *errUnexpectedClusterStatus) Error() string {
return fmt.Sprintf("unexpected error status: %s", e.status)
}
Loading