Skip to content

Commit

Permalink
Move packages to internal, fix linter findings (#22)
Browse files Browse the repository at this point in the history
  • Loading branch information
sbueringer authored Jun 5, 2023
1 parent b082da4 commit 2d9dd87
Show file tree
Hide file tree
Showing 67 changed files with 179 additions and 142 deletions.
9 changes: 8 additions & 1 deletion .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -165,4 +165,11 @@ issues:
# We are disabling default golangci exclusions because we want to help reviewers to focus on reviewing the most relevant
# changes in PRs and avoid nitpicking.
exclude-use-default: false
#exclude-rules:
# Just synced over some rules from core Cluster API to avoid fixing findings which are ignored there.
exclude-rules:
- linters:
- revive
text: "exported: exported method .*\\.(Reconcile|SetupWithManager|SetupWebhookWithManager) should have comment or be unexported"
- linters:
- errcheck
text: Error return value of .((os\.)?std(out|err)\..*|.*Close|.*Flush|os\.Remove(All)?|.*print(f|ln)?|os\.(Un)?Setenv). is not checked
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -93,11 +93,11 @@ generate-manifests: $(CONTROLLER_GEN) ## Generate manifests e.g. CRD, RBAC etc.

.PHONY: generate-go-deepcopy
generate-go-deepcopy: $(CONTROLLER_GEN) ## Generate deepcopy go code
$(MAKE) clean-generated-deepcopy SRC_DIRS="./api,./pkg/cloud/api"
$(MAKE) clean-generated-deepcopy SRC_DIRS="./api,./internal/cloud/api"
$(CONTROLLER_GEN) \
object:headerFile=./hack/boilerplate/boilerplate.generatego.txt \
paths=./api/... \
paths=./pkg/cloud/api/...
paths=./internal/cloud/api/...

.PHONY: generate-modules
generate-modules: ## Run go mod tidy to ensure modules are up to date
Expand Down
3 changes: 3 additions & 0 deletions api/v1alpha1/goofycluster_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,14 @@ const (
ClusterFinalizer = "goofycluster.infrastructure.cluster.x-k8s.io"
)

// GoofyClusterSpec defines the desired state of the GoofyCluster.
type GoofyClusterSpec struct {
// ControlPlaneEndpoint represents the endpoint used to communicate with the control plane.
// +optional
ControlPlaneEndpoint APIEndpoint `json:"controlPlaneEndpoint"`
}

// GoofyClusterStatus defines the observed state of the GoofyCluster.
type GoofyClusterStatus struct {
// Ready denotes that the goofy cluster (infrastructure) is ready.
// +optional
Expand Down Expand Up @@ -63,6 +65,7 @@ type APIEndpoint struct {
// +kubebuilder:printcolumn:name="Cluster",type="string",JSONPath=".metadata.labels['cluster\\.x-k8s\\.io/cluster-name']",description="Cluster"
// +kubebuilder:printcolumn:name="Age",type="date",JSONPath=".metadata.creationTimestamp",description="Time duration since creation of GoofyCluster"

// GoofyCluster is the Schema for the goofy clusters API.
type GoofyCluster struct {
metav1.TypeMeta `json:",inline"`
metav1.ObjectMeta `json:"metadata,omitempty"`
Expand Down
4 changes: 2 additions & 2 deletions controllers/alias.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@ import (
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/controller"

"github.com/fabriziopandini/cluster-api-provider-goofy/internal/cloud"
goofycontrollers "github.com/fabriziopandini/cluster-api-provider-goofy/internal/controllers"
"github.com/fabriziopandini/cluster-api-provider-goofy/pkg/cloud"
"github.com/fabriziopandini/cluster-api-provider-goofy/pkg/server"
"github.com/fabriziopandini/cluster-api-provider-goofy/internal/server"
)

// Following types provides access to reconcilers implemented in internal/controllers, thus
Expand Down
7 changes: 4 additions & 3 deletions pkg/cloud/alias.go → internal/cloud/alias.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,15 @@ package cloud
import (
"sigs.k8s.io/controller-runtime/pkg/client"

cbuilder "github.com/fabriziopandini/cluster-api-provider-goofy/pkg/cloud/runtime/builder"
cclient "github.com/fabriziopandini/cluster-api-provider-goofy/pkg/cloud/runtime/client"
cmanager "github.com/fabriziopandini/cluster-api-provider-goofy/pkg/cloud/runtime/manager"
cbuilder "github.com/fabriziopandini/cluster-api-provider-goofy/internal/cloud/runtime/builder"
cclient "github.com/fabriziopandini/cluster-api-provider-goofy/internal/cloud/runtime/client"
cmanager "github.com/fabriziopandini/cluster-api-provider-goofy/internal/cloud/runtime/manager"
)

// Client knows how to perform CRUD operations on resources in a resource group.
type Client cclient.Client

// Object represents an object.
type Object client.Object

// Manager initializes shared dependencies such as Caches and Clients, and provides them to Runnables.
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,20 @@ import (
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

// CloudMachineKind is the kind of CloudMachine.
const CloudMachineKind = "CloudMachine"

// CloudMachineSpec is the spec of CloudMachine.
type CloudMachineSpec struct {
}

// CloudMachineStatus is the status of CloudMachine.
type CloudMachineStatus struct {
}

// +kubebuilder:object:root=true

// CloudMachine represents a machine in the cloud.
type CloudMachine struct {
metav1.TypeMeta `json:",inline"`
metav1.ObjectMeta `json:"metadata,omitempty"`
Expand All @@ -40,6 +44,7 @@ type CloudMachine struct {

// +kubebuilder:object:root=true

// CloudMachineList contains a list of CloudMachine.
type CloudMachineList struct {
metav1.TypeMeta `json:",inline"`
metav1.ListMeta `json:"metadata,omitempty"`
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,12 @@ import (
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/client/apiutil"

ccontroller "github.com/fabriziopandini/cluster-api-provider-goofy/pkg/cloud/runtime/controller"
"github.com/fabriziopandini/cluster-api-provider-goofy/pkg/cloud/runtime/handler"
cmanager "github.com/fabriziopandini/cluster-api-provider-goofy/pkg/cloud/runtime/manager"
cpredicate "github.com/fabriziopandini/cluster-api-provider-goofy/pkg/cloud/runtime/predicate"
creconciler "github.com/fabriziopandini/cluster-api-provider-goofy/pkg/cloud/runtime/reconcile"
csource "github.com/fabriziopandini/cluster-api-provider-goofy/pkg/cloud/runtime/source"
ccontroller "github.com/fabriziopandini/cluster-api-provider-goofy/internal/cloud/runtime/controller"
"github.com/fabriziopandini/cluster-api-provider-goofy/internal/cloud/runtime/handler"
cmanager "github.com/fabriziopandini/cluster-api-provider-goofy/internal/cloud/runtime/manager"
cpredicate "github.com/fabriziopandini/cluster-api-provider-goofy/internal/cloud/runtime/predicate"
creconciler "github.com/fabriziopandini/cluster-api-provider-goofy/internal/cloud/runtime/reconcile"
csource "github.com/fabriziopandini/cluster-api-provider-goofy/internal/cloud/runtime/source"
)

// Builder builds a Controller.
Expand Down
18 changes: 18 additions & 0 deletions internal/cloud/runtime/builder/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 builder provides a builder to build controllers.
package builder
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ limitations under the License.
package builder

import (
cpredicate "github.com/fabriziopandini/cluster-api-provider-goofy/pkg/cloud/runtime/predicate"
cpredicate "github.com/fabriziopandini/cluster-api-provider-goofy/internal/cloud/runtime/predicate"
)

// ForOption is some configuration that modifies options for a For request.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import (
"sigs.k8s.io/controller-runtime/pkg/client/apiutil"
)

// Cache implements an in-memory cache.
// TODO: consider if to move to internal.
type Cache interface {
Start(ctx context.Context) error
Expand All @@ -52,10 +53,12 @@ type Cache interface {
GetInformerForKind(ctx context.Context, gvk schema.GroupVersionKind) (Informer, error)
}

// Informer forwards events to event handlers.
type Informer interface {
AddEventHandler(handler InformEventHandler) error
}

// InformEventHandler handle events originated by a source.
type InformEventHandler interface {
OnCreate(resourceGroup string, obj client.Object)
OnUpdate(resourceGroup string, oldObj, newObj client.Object)
Expand Down Expand Up @@ -110,6 +113,7 @@ func newOwnReferenceFromOwnerReference(namespace string, owner metav1.OwnerRefer

var _ Cache = &cache{}

// NewCache returns a new Cache.
func NewCache(scheme *runtime.Scheme) Cache {
return &cache{
scheme: scheme,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ import (
"k8s.io/klog/v2"
ctrl "sigs.k8s.io/controller-runtime"

cloudv1 "github.com/fabriziopandini/cluster-api-provider-goofy/pkg/cloud/api/v1alpha1"
cloudv1 "github.com/fabriziopandini/cluster-api-provider-goofy/internal/cloud/api/v1alpha1"
)

var scheme = runtime.NewScheme()
Expand All @@ -58,7 +58,6 @@ func Test_cache_scale(t *testing.T) {
var listCount uint64
var deleteCount uint64

rand.Seed(time.Now().UnixNano())
ctx, cancel := context.WithCancel(context.TODO())
defer cancel()

Expand All @@ -84,8 +83,8 @@ func Test_cache_scale(t *testing.T) {
for {
select {
case <-time.After(wait.Jitter(operationFrequencyForResourceGroup, 1)):
operation := rand.Intn(3)
item := rand.Intn(objectsForResourceGroups)
operation := rand.Intn(3) //nolint:gosec // Intentionally using a weak random number generator here.
item := rand.Intn(objectsForResourceGroups) //nolint:gosec // Intentionally using a weak random number generator here.
switch operation {
case 0: // create or get
machine := &cloudv1.CloudMachine{
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ import (
"k8s.io/client-go/util/workqueue"
"sigs.k8s.io/controller-runtime/pkg/client"

cloudv1 "github.com/fabriziopandini/cluster-api-provider-goofy/pkg/cloud/api/v1alpha1"
cloudv1 "github.com/fabriziopandini/cluster-api-provider-goofy/internal/cloud/api/v1alpha1"
)

func Test_cache_client(t *testing.T) {
Expand Down Expand Up @@ -733,7 +733,10 @@ func Test_cache_client(t *testing.T) {
})
}

//nolint:unparam // we want to have resourceGroup as parameter even though we currently always set it to the same value.
func createMachine(t *testing.T, c *cache, resourceGroup, name string) *cloudv1.CloudMachine {
t.Helper()

g := NewWithT(t)

obj := &cloudv1.CloudMachine{
Expand Down
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import (
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"

cloudv1 "github.com/fabriziopandini/cluster-api-provider-goofy/pkg/cloud/api/v1alpha1"
cloudv1 "github.com/fabriziopandini/cluster-api-provider-goofy/internal/cloud/api/v1alpha1"
)

func Test_cache_gc(t *testing.T) {
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import (
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"

cloudv1 "github.com/fabriziopandini/cluster-api-provider-goofy/pkg/cloud/api/v1alpha1"
cloudv1 "github.com/fabriziopandini/cluster-api-provider-goofy/internal/cloud/api/v1alpha1"
)

func Test_cache_sync(t *testing.T) {
Expand Down
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,10 @@ import (
"k8s.io/client-go/util/workqueue"
ctrl "sigs.k8s.io/controller-runtime"

chandler "github.com/fabriziopandini/cluster-api-provider-goofy/pkg/cloud/runtime/handler"
cpredicate "github.com/fabriziopandini/cluster-api-provider-goofy/pkg/cloud/runtime/predicate"
creconciler "github.com/fabriziopandini/cluster-api-provider-goofy/pkg/cloud/runtime/reconcile"
csource "github.com/fabriziopandini/cluster-api-provider-goofy/pkg/cloud/runtime/source"
chandler "github.com/fabriziopandini/cluster-api-provider-goofy/internal/cloud/runtime/handler"
cpredicate "github.com/fabriziopandini/cluster-api-provider-goofy/internal/cloud/runtime/predicate"
creconciler "github.com/fabriziopandini/cluster-api-provider-goofy/internal/cloud/runtime/reconcile"
csource "github.com/fabriziopandini/cluster-api-provider-goofy/internal/cloud/runtime/source"
)

// Options are the arguments for creating a new Controller.
Expand All @@ -39,6 +39,7 @@ type Options struct {
Reconciler creconciler.Reconciler
}

// Controller represents a controller.
type Controller interface {
Watch(src csource.Source, evthdler chandler.EventHandler, prct ...cpredicate.Predicate) error
Start(ctx context.Context) error
Expand Down Expand Up @@ -78,6 +79,7 @@ type controller struct {
startWatches []watchDescription
started bool

//nolint:containedctx
ctx context.Context
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,11 @@ import (
"k8s.io/apimachinery/pkg/types"
"sigs.k8s.io/controller-runtime/pkg/client"

cloudv1 "github.com/fabriziopandini/cluster-api-provider-goofy/pkg/cloud/api/v1alpha1"
ccache "github.com/fabriziopandini/cluster-api-provider-goofy/pkg/cloud/runtime/cache"
chandler "github.com/fabriziopandini/cluster-api-provider-goofy/pkg/cloud/runtime/handler"
creconciler "github.com/fabriziopandini/cluster-api-provider-goofy/pkg/cloud/runtime/reconcile"
csource "github.com/fabriziopandini/cluster-api-provider-goofy/pkg/cloud/runtime/source"
cloudv1 "github.com/fabriziopandini/cluster-api-provider-goofy/internal/cloud/api/v1alpha1"
ccache "github.com/fabriziopandini/cluster-api-provider-goofy/internal/cloud/runtime/cache"
chandler "github.com/fabriziopandini/cluster-api-provider-goofy/internal/cloud/runtime/handler"
creconciler "github.com/fabriziopandini/cluster-api-provider-goofy/internal/cloud/runtime/reconcile"
csource "github.com/fabriziopandini/cluster-api-provider-goofy/internal/cloud/runtime/source"
)

var scheme = runtime.NewScheme()
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ import (
"k8s.io/apimachinery/pkg/types"
"k8s.io/client-go/util/workqueue"

cevent "github.com/fabriziopandini/cluster-api-provider-goofy/pkg/cloud/runtime/event"
creconciler "github.com/fabriziopandini/cluster-api-provider-goofy/pkg/cloud/runtime/reconcile"
cevent "github.com/fabriziopandini/cluster-api-provider-goofy/internal/cloud/runtime/event"
creconciler "github.com/fabriziopandini/cluster-api-provider-goofy/internal/cloud/runtime/reconcile"
)

var _ EventHandler = &EnqueueRequestForObject{}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ package handler
import (
"k8s.io/client-go/util/workqueue"

cevent "github.com/fabriziopandini/cluster-api-provider-goofy/pkg/cloud/runtime/event"
cevent "github.com/fabriziopandini/cluster-api-provider-goofy/internal/cloud/runtime/event"
)

// EventHandler enqueues reconcile.Requests in response to events (e.g. object Create). EventHandlers map an Event
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@ import (
"k8s.io/apimachinery/pkg/runtime"
ctrl "sigs.k8s.io/controller-runtime"

ccache "github.com/fabriziopandini/cluster-api-provider-goofy/pkg/cloud/runtime/cache"
ccontroller "github.com/fabriziopandini/cluster-api-provider-goofy/pkg/cloud/runtime/controller"
cresourcegroup "github.com/fabriziopandini/cluster-api-provider-goofy/pkg/cloud/runtime/resourcegroup"
ccache "github.com/fabriziopandini/cluster-api-provider-goofy/internal/cloud/runtime/cache"
ccontroller "github.com/fabriziopandini/cluster-api-provider-goofy/internal/cloud/runtime/controller"
cresourcegroup "github.com/fabriziopandini/cluster-api-provider-goofy/internal/cloud/runtime/resourcegroup"
)

// Manager initializes shared dependencies such as Caches and Clients, and provides them to Runnables.
Expand Down Expand Up @@ -57,6 +57,7 @@ type manager struct {
started bool
}

// New creates a new manager.
func New(scheme *runtime.Scheme) Manager {
m := &manager{
scheme: scheme,
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ limitations under the License.
package predicate

import (
cevent "github.com/fabriziopandini/cluster-api-provider-goofy/pkg/cloud/runtime/event"
cevent "github.com/fabriziopandini/cluster-api-provider-goofy/internal/cloud/runtime/event"
)

// Predicate filters events before enqueuing the keys.
Expand Down
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ import (

"sigs.k8s.io/controller-runtime/pkg/client"

ccache "github.com/fabriziopandini/cluster-api-provider-goofy/pkg/cloud/runtime/cache"
cclient "github.com/fabriziopandini/cluster-api-provider-goofy/pkg/cloud/runtime/client"
ccache "github.com/fabriziopandini/cluster-api-provider-goofy/internal/cloud/runtime/cache"
cclient "github.com/fabriziopandini/cluster-api-provider-goofy/internal/cloud/runtime/client"
)

var _ ResourceGroup = &cachedResourceGroup{}
Expand All @@ -32,6 +32,7 @@ type cachedResourceGroup struct {
cache ccache.Cache
}

// NewResourceGroup returns a new resource group.
func NewResourceGroup(name string, cache ccache.Cache) ResourceGroup {
return &cachedResourceGroup{
name: name,
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,10 @@ limitations under the License.
package resourcegroup

import (
cclient "github.com/fabriziopandini/cluster-api-provider-goofy/pkg/cloud/runtime/client"
cclient "github.com/fabriziopandini/cluster-api-provider-goofy/internal/cloud/runtime/client"
)

// ResourceGroup groups resources for a workload cluster.
type ResourceGroup interface {
GetClient() cclient.Client
}
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,10 @@ import (
"k8s.io/client-go/util/workqueue"
"sigs.k8s.io/controller-runtime/pkg/client"

ccache "github.com/fabriziopandini/cluster-api-provider-goofy/pkg/cloud/runtime/cache"
cevent "github.com/fabriziopandini/cluster-api-provider-goofy/pkg/cloud/runtime/event"
chandler "github.com/fabriziopandini/cluster-api-provider-goofy/pkg/cloud/runtime/handler"
cpredicate "github.com/fabriziopandini/cluster-api-provider-goofy/pkg/cloud/runtime/predicate"
ccache "github.com/fabriziopandini/cluster-api-provider-goofy/internal/cloud/runtime/cache"
cevent "github.com/fabriziopandini/cluster-api-provider-goofy/internal/cloud/runtime/event"
chandler "github.com/fabriziopandini/cluster-api-provider-goofy/internal/cloud/runtime/handler"
cpredicate "github.com/fabriziopandini/cluster-api-provider-goofy/internal/cloud/runtime/predicate"
)

// Informer is used to provide a source of events originating from CRUD operations inside the resourceGroup (e.g. Resource Create).
Expand Down
Loading

0 comments on commit 2d9dd87

Please sign in to comment.