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

Refactor cloud services into Reconcile/Delete pattern #394

Merged
merged 1 commit into from
Jun 30, 2021
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
22 changes: 22 additions & 0 deletions cloud/defaults.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/*
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.
See the License for the specific language governing permissions and
limitations under the License.
*/

package cloud

const (
// ProviderIDPrefix is the gce provider id prefix.
ProviderIDPrefix = "gce://"
)
18 changes: 18 additions & 0 deletions cloud/doc.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/*
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.
See the License for the specific language governing permissions and
limitations under the License.
*/

// Package cloud implement cloud resources lifecycle.
package cloud
10 changes: 10 additions & 0 deletions cloud/gcperrors/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,13 @@ func IsNotFound(err error) bool {

return ok && ae.Code == http.StatusNotFound
}

// IgnoreNotFound ignore Google API not found error and return nil.
// Otherwise return the actual error.
func IgnoreNotFound(err error) error {
if IsNotFound(err) {
return nil
}

return err
}
98 changes: 98 additions & 0 deletions cloud/interfaces.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
/*
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.
See the License for the specific language governing permissions and
limitations under the License.
*/

package cloud

import (
"context"

"github.com/GoogleCloudPlatform/k8s-cloud-provider/pkg/cloud"

corev1 "k8s.io/api/core/v1"
infrav1 "sigs.k8s.io/cluster-api-provider-gcp/api/v1alpha4"
clusterv1 "sigs.k8s.io/cluster-api/api/v1alpha4"
capierrors "sigs.k8s.io/cluster-api/errors"
)

// Cloud alias for cloud.Cloud interface.
type Cloud = cloud.Cloud

// Reconciler is a generic interface used by components offering a type of service.
type Reconciler interface {
Reconcile(ctx context.Context) error
Delete(ctx context.Context) error
}

// Client is an interface which can get cloud client.
type Client interface {
Cloud() Cloud
}

// ClusterGetter is an interface which can get cluster informations.
type ClusterGetter interface {
Client
Project() string
Region() string
Name() string
Namespace() string
NetworkName() string
Network() *infrav1.Network
AdditionalLabels() infrav1.Labels
FailureDomains() clusterv1.FailureDomains
ControlPlaneEndpoint() clusterv1.APIEndpoint
}

// ClusterSetter is an interface which can set cluster informations.
type ClusterSetter interface {
SetControlPlaneEndpoint(endpoint clusterv1.APIEndpoint)
}

// Cluster is an interface which can get and set cluster informations.
type Cluster interface {
ClusterGetter
ClusterSetter
}

// MachineGetter is an interface which can get machine informations.
type MachineGetter interface {
Client
Name() string
Namespace() string
Zone() string
Role() string
IsControlPlane() bool
ControlPlaneGroupName() string
GetInstanceID() *string
GetProviderID() string
GetBootstrapData() (string, error)
GetInstanceStatus() *infrav1.InstanceStatus
}

// MachineSetter is an interface which can set machine informations.
type MachineSetter interface {
SetProviderID()
SetInstanceStatus(v infrav1.InstanceStatus)
SetFailureMessage(v error)
SetFailureReason(v capierrors.MachineStatusError)
SetAnnotation(key, value string)
SetAddresses(addressList []corev1.NodeAddress)
}

// Machine is an interface which can get and set machine informations.
type Machine interface {
MachineGetter
MachineSetter
}
43 changes: 40 additions & 3 deletions cloud/scope/clients.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,47 @@ limitations under the License.
package scope

import (
"context"
"time"

"github.com/GoogleCloudPlatform/k8s-cloud-provider/pkg/cloud"
computebeta "google.golang.org/api/compute/v0.beta"
"google.golang.org/api/compute/v1"

"k8s.io/client-go/util/flowcontrol"
)

// GCPClients contains all the gcp clients used by the scopes.
type GCPClients struct {
Compute *compute.Service
// GCPServices contains all the gcp services used by the scopes.
type GCPServices struct {
Compute *compute.Service
ComputeBeta *computebeta.Service
}

// GCPRateLimiter implements cloud.RateLimiter.
type GCPRateLimiter struct{}

// Accept blocks until the operation can be performed.
func (rl *GCPRateLimiter) Accept(ctx context.Context, key *cloud.RateLimitKey) error {
if key.Operation == "Get" && key.Service == "Operations" {
// Wait a minimum amount of time regardless of rate limiter.
rl := &cloud.MinimumRateLimiter{
// Convert flowcontrol.RateLimiter into cloud.RateLimiter
RateLimiter: &cloud.AcceptRateLimiter{
Acceptor: flowcontrol.NewTokenBucketRateLimiter(5, 5), // 5
},
Minimum: time.Second,
}

return rl.Accept(ctx, key)
}
return nil
}

func newCloud(project string, service GCPServices) cloud.Cloud {
return cloud.NewGCE(&cloud.Service{
GA: service.Compute,
Beta: service.ComputeBeta,
ProjectRouter: &cloud.SingleProjectRouter{ID: project},
RateLimiter: &GCPRateLimiter{},
})
}
Loading