Skip to content

Commit

Permalink
Merge pull request #1962 from justinsb/refactor_config
Browse files Browse the repository at this point in the history
chore: Refactor ControllerConfig into its own package
  • Loading branch information
google-oss-prow[bot] committed Jun 6, 2024
2 parents d44ff80 + 50c530a commit 5c145ea
Show file tree
Hide file tree
Showing 19 changed files with 98 additions and 81 deletions.
4 changes: 2 additions & 2 deletions pkg/cli/stream/url_to_unstructured_resource_stream.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import (
"net/http"

"github.com/GoogleCloudPlatform/k8s-config-connector/pkg/cli/gcpclient"
"github.com/GoogleCloudPlatform/k8s-config-connector/pkg/controller"
"github.com/GoogleCloudPlatform/k8s-config-connector/pkg/config"
"github.com/GoogleCloudPlatform/k8s-config-connector/pkg/controller/direct"
"github.com/GoogleCloudPlatform/k8s-config-connector/pkg/resourceskeleton"
"github.com/GoogleCloudPlatform/k8s-config-connector/pkg/servicemapping/servicemappingloader"
Expand Down Expand Up @@ -56,7 +56,7 @@ func (s *URLToUnstructuredResourceStream) Next(ctx context.Context) (*unstructur
}

// First check if this resource uses our direct-reconciliation model
exported, err := direct.Export(ctx, s.url, &controller.Config{
exported, err := direct.Export(ctx, s.url, &config.ControllerConfig{
HTTPClient: s.httpClient,
})
if err != nil {
Expand Down
34 changes: 34 additions & 0 deletions pkg/config/controllerconfig.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// Copyright 2024 Google LLC
//
// 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 config

import "net/http"

type ControllerConfig struct {
UserAgent string

// UserProjectOverride provides the option to use the resource project for preconditions, quota, and billing,
// instead of the project the credentials belong to; false by default
UserProjectOverride bool

// BillingProject is the project used by the TF provider and DCL client to determine preconditions,
// quota, and billing if UserProjectOverride is set to true. If this field is empty,
// but UserProjectOverride is set to true, resource project will be used.
BillingProject string

// HTTPClient allows us to specify the HTTP client to use with DCL.
// This is particularly useful in mocks/tests.
HTTPClient *http.Client
}
19 changes: 0 additions & 19 deletions pkg/controller/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@
package controller

import (
"net/http"

"github.com/GoogleCloudPlatform/declarative-resource-client-library/dcl"
"github.com/GoogleCloudPlatform/k8s-config-connector/pkg/controller/jitter"
"github.com/GoogleCloudPlatform/k8s-config-connector/pkg/dcl/conversion"
Expand All @@ -25,23 +23,6 @@ import (
tfschema "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)

type Config struct {
UserAgent string

// UserProjectOverride provides the option to use the resource project for preconditions, quota, and billing,
// instead of the project the credentials belong to; false by default
UserProjectOverride bool

// BillingProject is the project used by the TF provider and DCL client to determine preconditions,
// quota, and billing if UserProjectOverride is set to true. If this field is empty,
// but UserProjectOverride is set to true, resource project will be used.
BillingProject string

// HTTPClient allows us to specify the HTTP client to use with DCL.
// This is particularly useful in mocks/tests.
HTTPClient *http.Client
}

// Common controller dependencies.
type Deps struct {
TfProvider *tfschema.Provider
Expand Down
6 changes: 3 additions & 3 deletions pkg/controller/direct/alloydb/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,16 @@ import (
"context"
"fmt"

"github.com/GoogleCloudPlatform/k8s-config-connector/pkg/controller"
"github.com/GoogleCloudPlatform/k8s-config-connector/pkg/config"
api "google.golang.org/api/alloydb/v1beta"
"google.golang.org/api/option"
)

type gcpClient struct {
config controller.Config
config config.ControllerConfig
}

func newGCPClient(ctx context.Context, config *controller.Config) (*gcpClient, error) {
func newGCPClient(ctx context.Context, config *config.ControllerConfig) (*gcpClient, error) {
gcpClient := &gcpClient{
config: *config,
}
Expand Down
6 changes: 3 additions & 3 deletions pkg/controller/direct/alloydb/cluster_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,21 +30,21 @@ import (
"sigs.k8s.io/controller-runtime/pkg/client"

krm "github.com/GoogleCloudPlatform/k8s-config-connector/pkg/clients/generated/apis/alloydb/v1beta1"
"github.com/GoogleCloudPlatform/k8s-config-connector/pkg/controller"
"github.com/GoogleCloudPlatform/k8s-config-connector/pkg/config"
"github.com/GoogleCloudPlatform/k8s-config-connector/pkg/controller/direct/directbase"
)

func init() {
directbase.ControllerBuilder.RegisterModel(krm.AlloyDBClusterGVK, NewModel)
}

func NewModel(config *controller.Config) directbase.Model {
func NewModel(config *config.ControllerConfig) directbase.Model {
return &clusterModel{config: config}
}

type clusterModel struct {
// *gcpClient
config *controller.Config
config *config.ControllerConfig
}

// model implements the Model interface.
Expand Down
6 changes: 3 additions & 3 deletions pkg/controller/direct/apikeys/apikeyskey_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ import (
"sigs.k8s.io/controller-runtime/pkg/client"

krm "github.com/GoogleCloudPlatform/k8s-config-connector/pkg/clients/generated/apis/apikeys/v1alpha1"
"github.com/GoogleCloudPlatform/k8s-config-connector/pkg/controller"
"github.com/GoogleCloudPlatform/k8s-config-connector/pkg/config"
"github.com/GoogleCloudPlatform/k8s-config-connector/pkg/controller/direct/directbase"

. "github.com/GoogleCloudPlatform/k8s-config-connector/pkg/controller/direct/mappings" //nolint:revive
Expand All @@ -39,12 +39,12 @@ func init() {
directbase.ControllerBuilder.RegisterModel(krm.APIKeysKeyGVK, newAPIKeysModel)
}

func newAPIKeysModel(config *controller.Config) directbase.Model {
func newAPIKeysModel(config *config.ControllerConfig) directbase.Model {
return &model{config: *config}
}

type model struct {
config controller.Config
config config.ControllerConfig
}

// model implements the Model interface.
Expand Down
12 changes: 6 additions & 6 deletions pkg/controller/direct/directbase/directbase_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import (

"github.com/GoogleCloudPlatform/k8s-config-connector/operator/pkg/apis/core/v1beta1"
"github.com/GoogleCloudPlatform/k8s-config-connector/operator/pkg/kccstate"
"github.com/GoogleCloudPlatform/k8s-config-connector/pkg/controller"
"github.com/GoogleCloudPlatform/k8s-config-connector/pkg/config"
kcciamclient "github.com/GoogleCloudPlatform/k8s-config-connector/pkg/controller/iam/iamclient"
"github.com/GoogleCloudPlatform/k8s-config-connector/pkg/controller/jitter"
"github.com/GoogleCloudPlatform/k8s-config-connector/pkg/controller/lifecyclehandler"
Expand Down Expand Up @@ -62,17 +62,17 @@ func init() {
}

type directControllerBuilder struct {
modelMapper map[schema.GroupVersionKind]func(*controller.Config) Model
modelMapper map[schema.GroupVersionKind]func(*config.ControllerConfig) Model
}

func (c *directControllerBuilder) RegisterModel(gvk schema.GroupVersionKind, modelFn func(*controller.Config) Model) {
func (c *directControllerBuilder) RegisterModel(gvk schema.GroupVersionKind, modelFn func(*config.ControllerConfig) Model) {
if c.modelMapper == nil {
c.modelMapper = map[schema.GroupVersionKind]func(*controller.Config) Model{}
c.modelMapper = map[schema.GroupVersionKind]func(*config.ControllerConfig) Model{}
}
c.modelMapper[gvk] = modelFn
}

func (c *directControllerBuilder) AddController(mgr manager.Manager, config *controller.Config, crd *apiextensions.CustomResourceDefinition, deps Deps) error {
func (c *directControllerBuilder) AddController(mgr manager.Manager, config *config.ControllerConfig, crd *apiextensions.CustomResourceDefinition, deps Deps) error {
immediateReconcileRequests := make(chan event.GenericEvent, k8s.ImmediateReconcileRequestsBufferSize)
resourceWatcherRoutines := semaphore.NewWeighted(k8s.MaxNumResourceWatcherRoutines)

Expand Down Expand Up @@ -102,7 +102,7 @@ func (c *directControllerBuilder) gvkByCrd(crd *apiextensions.CustomResourceDefi
}

// NewReconciler returns a new reconcile.Reconciler.
func (c *directControllerBuilder) NewReconciler(mgr manager.Manager, config *controller.Config, immediateReconcileRequests chan event.GenericEvent, resourceWatcherRoutines *semaphore.Weighted,
func (c *directControllerBuilder) NewReconciler(mgr manager.Manager, config *config.ControllerConfig, immediateReconcileRequests chan event.GenericEvent, resourceWatcherRoutines *semaphore.Weighted,
crd *apiextensions.CustomResourceDefinition, jg jitter.Generator) (*DirectReconciler, error) {
gvk := c.gvkByCrd(crd)
if gvk.Empty() {
Expand Down
6 changes: 3 additions & 3 deletions pkg/controller/direct/gkehub/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,16 @@ import (
"context"
"fmt"

"github.com/GoogleCloudPlatform/k8s-config-connector/pkg/controller"
"github.com/GoogleCloudPlatform/k8s-config-connector/pkg/config"
featureapi "google.golang.org/api/gkehub/v1beta"
"google.golang.org/api/option"
)

type gcpClient struct {
config controller.Config
config config.ControllerConfig
}

func newGCPClient(config *controller.Config) (*gcpClient, error) {
func newGCPClient(config *config.ControllerConfig) (*gcpClient, error) {
gcpClient := &gcpClient{
config: *config,
}
Expand Down
6 changes: 3 additions & 3 deletions pkg/controller/direct/gkehub/featuremembership_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import (

refs "github.com/GoogleCloudPlatform/k8s-config-connector/apis/refs/v1beta1"
krm "github.com/GoogleCloudPlatform/k8s-config-connector/pkg/clients/generated/apis/gkehub/v1beta1"
"github.com/GoogleCloudPlatform/k8s-config-connector/pkg/controller"
"github.com/GoogleCloudPlatform/k8s-config-connector/pkg/config"
"github.com/GoogleCloudPlatform/k8s-config-connector/pkg/controller/direct/directbase"
"github.com/GoogleCloudPlatform/k8s-config-connector/pkg/controller/direct/references"
)
Expand All @@ -38,12 +38,12 @@ func init() {
directbase.ControllerBuilder.RegisterModel(krm.GKEHubFeatureMembershipGVK, GetModel)
}

func GetModel(config *controller.Config) directbase.Model {
func GetModel(config *config.ControllerConfig) directbase.Model {
return &gkeHubModel{config: config}
}

type gkeHubModel struct {
config *controller.Config
config *config.ControllerConfig
}

// model implements the Model interface.
Expand Down
6 changes: 3 additions & 3 deletions pkg/controller/direct/logging/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,16 @@ import (
"context"
"fmt"

"github.com/GoogleCloudPlatform/k8s-config-connector/pkg/controller"
"github.com/GoogleCloudPlatform/k8s-config-connector/pkg/config"
api "google.golang.org/api/logging/v2"
"google.golang.org/api/option"
)

type gcpClient struct {
config controller.Config
config config.ControllerConfig
}

func newGCPClient(ctx context.Context, config *controller.Config) (*gcpClient, error) {
func newGCPClient(ctx context.Context, config *config.ControllerConfig) (*gcpClient, error) {
gcpClient := &gcpClient{
config: *config,
}
Expand Down
6 changes: 3 additions & 3 deletions pkg/controller/direct/logging/logmetric_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import (

krm "github.com/GoogleCloudPlatform/k8s-config-connector/apis/resources/logging/v1beta1"
"github.com/GoogleCloudPlatform/k8s-config-connector/pkg/apis/k8s/v1alpha1"
"github.com/GoogleCloudPlatform/k8s-config-connector/pkg/controller"
"github.com/GoogleCloudPlatform/k8s-config-connector/pkg/config"
"github.com/GoogleCloudPlatform/k8s-config-connector/pkg/controller/direct/directbase"
"github.com/GoogleCloudPlatform/k8s-config-connector/pkg/controller/direct/references"
)
Expand All @@ -39,12 +39,12 @@ func init() {
directbase.ControllerBuilder.RegisterModel(krm.LoggingLogMetricGVK, NewLogMetricModel)
}

func NewLogMetricModel(config *controller.Config) directbase.Model {
func NewLogMetricModel(config *config.ControllerConfig) directbase.Model {
return &logMetricModel{config: config}
}

type logMetricModel struct {
config *controller.Config
config *config.ControllerConfig
}

// model implements the Model interface.
Expand Down
4 changes: 2 additions & 2 deletions pkg/controller/direct/registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import (
"fmt"
"strings"

"github.com/GoogleCloudPlatform/k8s-config-connector/pkg/controller"
"github.com/GoogleCloudPlatform/k8s-config-connector/pkg/config"
"github.com/GoogleCloudPlatform/k8s-config-connector/pkg/controller/direct/logging"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime/schema"
Expand Down Expand Up @@ -48,7 +48,7 @@ func SupportsIAM(groupKind schema.GroupKind) (bool, error) {
// Export attempts to export the resource specified by url.
// The url format should match the Cloud-Asset-Inventory format: https://cloud.google.com/asset-inventory/docs/resource-name-format
// If url is not recognized or not implemented by a direct controller, this returns (nil, nil)
func Export(ctx context.Context, url string, config *controller.Config) (*unstructured.Unstructured, error) {
func Export(ctx context.Context, url string, config *config.ControllerConfig) (*unstructured.Unstructured, error) {
if strings.HasPrefix(url, "//logging.googleapis.com/") {
tokens := strings.Split(strings.TrimPrefix(url, "//logging.googleapis.com/"), "/")
if len(tokens) == 4 && tokens[0] == "projects" && tokens[2] == "metrics" {
Expand Down
6 changes: 3 additions & 3 deletions pkg/controller/direct/resourcemanager/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,15 @@ import (
"fmt"

api "cloud.google.com/go/resourcemanager/apiv3"
"github.com/GoogleCloudPlatform/k8s-config-connector/pkg/controller"
"github.com/GoogleCloudPlatform/k8s-config-connector/pkg/config"
"google.golang.org/api/option"
)

type gcpClient struct {
config controller.Config
config config.ControllerConfig
}

func newGCPClient(ctx context.Context, config *controller.Config) (*gcpClient, error) {
func newGCPClient(ctx context.Context, config *config.ControllerConfig) (*gcpClient, error) {
gcpClient := &gcpClient{
config: *config,
}
Expand Down
6 changes: 3 additions & 3 deletions pkg/controller/direct/resourcemanager/tagkey_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,20 +30,20 @@ import (
"sigs.k8s.io/controller-runtime/pkg/client"

krm "github.com/GoogleCloudPlatform/k8s-config-connector/pkg/clients/generated/apis/tags/v1beta1"
"github.com/GoogleCloudPlatform/k8s-config-connector/pkg/controller"
"github.com/GoogleCloudPlatform/k8s-config-connector/pkg/config"
"github.com/GoogleCloudPlatform/k8s-config-connector/pkg/controller/direct/directbase"
)

func init() {
directbase.ControllerBuilder.RegisterModel(krm.TagsTagKeyGVK, newTagKeyModel)
}

func newTagKeyModel(config *controller.Config) directbase.Model {
func newTagKeyModel(config *config.ControllerConfig) directbase.Model {
return &tagKeyModel{config: config}
}

type tagKeyModel struct {
config *controller.Config
config *config.ControllerConfig
}

// model implements the Model interface.
Expand Down
Loading

0 comments on commit 5c145ea

Please sign in to comment.