-
Notifications
You must be signed in to change notification settings - Fork 238
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1955 from justinsb/refactor_registry
chore: Refactor registry to be more reusable
- Loading branch information
Showing
13 changed files
with
184 additions
and
100 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
// 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 registry | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
|
||
"github.com/GoogleCloudPlatform/k8s-config-connector/pkg/config" | ||
"github.com/GoogleCloudPlatform/k8s-config-connector/pkg/controller/direct/directbase" | ||
"k8s.io/apimachinery/pkg/runtime/schema" | ||
) | ||
|
||
var singleton = registry{} | ||
|
||
type registry struct { | ||
registrations map[schema.GroupKind]*registration | ||
} | ||
|
||
type registration struct { | ||
gvk schema.GroupVersionKind | ||
factory ModelFactoryFunc | ||
model directbase.Model | ||
} | ||
|
||
type ModelFactoryFunc func(ctx context.Context, config *config.ControllerConfig) (directbase.Model, error) | ||
|
||
func GetModel(gk schema.GroupKind) (directbase.Model, error) { | ||
registration := singleton.registrations[gk] | ||
if registration == nil { | ||
return nil, fmt.Errorf("no model registered for %s", gk) | ||
} | ||
return registration.model, nil | ||
} | ||
|
||
func PreferredGVK(gk schema.GroupKind) (schema.GroupVersionKind, bool) { | ||
registration := singleton.registrations[gk] | ||
if registration == nil { | ||
return schema.GroupVersionKind{}, false | ||
} | ||
return registration.gvk, true | ||
} | ||
|
||
func Init(ctx context.Context, config *config.ControllerConfig) error { | ||
for _, registration := range singleton.registrations { | ||
model, err := registration.factory(ctx, config) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
registration.model = model | ||
} | ||
return nil | ||
} | ||
|
||
func RegisterModel(gvk schema.GroupVersionKind, modelFn ModelFactoryFunc) { | ||
if singleton.registrations == nil { | ||
singleton.registrations = make(map[schema.GroupKind]*registration) | ||
} | ||
singleton.registrations[gvk.GroupKind()] = ®istration{ | ||
gvk: gvk, | ||
factory: modelFn, | ||
} | ||
} | ||
|
||
func IsDirectByGK(gk schema.GroupKind) bool { | ||
registration := singleton.registrations[gk] | ||
return registration != nil | ||
} | ||
|
||
// IsIAMDirect returns true if this resource uses the direct-reconciliation model for IAM. | ||
func IsIAMDirect(groupKind schema.GroupKind) bool { | ||
registration := singleton.registrations[groupKind] | ||
if registration == nil { | ||
return false | ||
} | ||
|
||
// TODO: Move to registration somehow? | ||
switch groupKind { | ||
case schema.GroupKind{Group: "privateca.cnrm.cloud.google.com", Kind: "PrivateCACAPool"}: | ||
return true | ||
} | ||
return false | ||
} | ||
|
||
// SupportsIAM returns true if this resource supports IAM (not all GCP resources do). | ||
// An error will be returned if IsDirect(groupKind) is not true. | ||
func SupportsIAM(groupKind schema.GroupKind) (bool, error) { | ||
// TODO: Move to registration somehow? | ||
switch groupKind { | ||
case schema.GroupKind{Group: "logging.cnrm.cloud.google.com", Kind: "LoggingLogMetric"}: | ||
return false, nil | ||
} | ||
return false, fmt.Errorf("groupKind %v is not recognized as a direct kind", groupKind) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.