Skip to content

Commit

Permalink
Merge pull request #2346 from acpana/acpana/maputils2
Browse files Browse the repository at this point in the history
refactor: maputils
  • Loading branch information
google-oss-prow[bot] authored Jul 24, 2024
2 parents 59b46c1 + 6292536 commit 80cd23e
Show file tree
Hide file tree
Showing 27 changed files with 511 additions and 1,080 deletions.
18 changes: 15 additions & 3 deletions pkg/controller/direct/alloydb/cluster_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ func (m *clusterModel) AdapterForObject(ctx context.Context, reader client.Reade
if err := runtime.DefaultUnstructuredConverter.FromUnstructured(u.Object, &obj); err != nil {
return nil, fmt.Errorf("error converting to %T: %w", obj, err)
}
resourceID := ValueOf(obj.Spec.ResourceID)
resourceID := direct.ValueOf(obj.Spec.ResourceID)
if resourceID == "" {
resourceID = obj.GetName()
}
Expand Down Expand Up @@ -125,7 +125,7 @@ func (a *clusterAdapter) Find(ctx context.Context) (bool, error) {
}
cluster, err := a.client.Projects.Locations.Clusters.Get(a.fullyQualifiedName()).Context(ctx).Do()
if err != nil {
if IsNotFound(err) {
if direct.IsNotFound(err) {
return false, nil
}
return false, err
Expand All @@ -145,7 +145,7 @@ func (a *clusterAdapter) Delete(ctx context.Context) (bool, error) {

op, err := a.client.Projects.Locations.Clusters.Delete(a.fullyQualifiedName()).Context(ctx).Do()
if err != nil {
if IsNotFound(err) {
if direct.IsNotFound(err) {
return false, nil
}
return false, fmt.Errorf("deleting cluster %s: %w", a.fullyQualifiedName(), err)
Expand Down Expand Up @@ -303,3 +303,15 @@ func (a *clusterAdapter) Export(ctx context.Context) (*unstructured.Unstructured
func (a *clusterAdapter) fullyQualifiedName() string {
return fmt.Sprintf("projects/%s/locations/%s/clusters/%s", a.projectID, a.location, a.resourceID)
}

func setStatus(u *unstructured.Unstructured, typedStatus any) error {
// TODO: Just fetch this object?
status, err := runtime.DefaultUnstructuredConverter.ToUnstructured(typedStatus)
if err != nil {
return fmt.Errorf("error converting status to unstructured: %w", err)
}
// TODO: Merge to avoid overwriting conditions?
u.Object["status"] = status

return nil
}
32 changes: 16 additions & 16 deletions pkg/controller/direct/alloydb/mapping.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@ func ClusterSpecToApi(ctx *direct.MapContext, in *krm.AlloyDBClusterSpec) *api.C
}
out := &api.Cluster{
AutomatedBackupPolicy: AutomatedBackupPolicy_KRMToApi(ctx, in.AutomatedBackupPolicy),
ClusterType: ValueOf(in.ClusterType),
ClusterType: direct.ValueOf(in.ClusterType),
ContinuousBackupConfig: ContinuousBackupConfig_KRMToApi(ctx, in.ContinuousBackupConfig),
DisplayName: ValueOf(in.DisplayName),
DisplayName: direct.ValueOf(in.DisplayName),
EncryptionConfig: EncryptionConfig_KRMToApi(ctx, in.EncryptionConfig),
InitialUser: InitialUser_KRMToApi(ctx, in.InitialUser),
NetworkConfig: NetworkConfig_KRMToApi(ctx, in.NetworkConfig),
Expand All @@ -45,7 +45,7 @@ func NetworkConfig_KRMToApi(ctx *direct.MapContext, in *krm.ClusterNetworkConfig
return nil
}
out := &api.NetworkConfig{
AllocatedIpRange: ValueOf(in.AllocatedIpRange),
AllocatedIpRange: direct.ValueOf(in.AllocatedIpRange),
Network: in.NetworkRef.External,
}
return out
Expand All @@ -57,11 +57,11 @@ func AutomatedBackupPolicy_KRMToApi(ctx *direct.MapContext, in *krm.ClusterAutom
return nil
}
out := &api.AutomatedBackupPolicy{
BackupWindow: ValueOf(in.BackupWindow),
Enabled: ValueOf(in.Enabled),
BackupWindow: direct.ValueOf(in.BackupWindow),
Enabled: direct.ValueOf(in.Enabled),
EncryptionConfig: EncryptionConfig_KRMToApi(ctx, in.EncryptionConfig),
Labels: in.Labels,
Location: ValueOf(in.Location),
Location: direct.ValueOf(in.Location),
QuantityBasedRetention: QuantityBasedRetention_KRMToApi(ctx, in.QuantityBasedRetention),
TimeBasedRetention: TimeBasedRetention_KRMToApi(ctx, in.TimeBasedRetention),
WeeklySchedule: WeeklySchedule_KRMToApi(ctx, in.WeeklySchedule),
Expand Down Expand Up @@ -94,10 +94,10 @@ func Time_KRMToApi(ctx *direct.MapContext, in *krm.ClusterStartTimes) *api.Googl
return nil
}
out := &api.GoogleTypeTimeOfDay{
Hours: int64(ValueOf(in.Hours)),
Minutes: int64(ValueOf(in.Minutes)),
Nanos: int64(ValueOf(in.Nanos)),
Seconds: int64(ValueOf(in.Seconds)),
Hours: int64(direct.ValueOf(in.Hours)),
Minutes: int64(direct.ValueOf(in.Minutes)),
Nanos: int64(direct.ValueOf(in.Nanos)),
Seconds: int64(direct.ValueOf(in.Seconds)),
}
return out
}
Expand All @@ -117,7 +117,7 @@ func TimeBasedRetention_KRMToApi(ctx *direct.MapContext, in *krm.ClusterTimeBase
return nil
}
out := &api.TimeBasedRetention{
RetentionPeriod: ValueOf(in.RetentionPeriod),
RetentionPeriod: direct.ValueOf(in.RetentionPeriod),
}

return out
Expand All @@ -128,7 +128,7 @@ func QuantityBasedRetention_KRMToApi(ctx *direct.MapContext, in *krm.ClusterQuan
return nil
}
out := &api.QuantityBasedRetention{
Count: int64(ValueOf(in.Count)),
Count: int64(direct.ValueOf(in.Count)),
}

return out
Expand All @@ -139,9 +139,9 @@ func ContinuousBackupConfig_KRMToApi(ctx *direct.MapContext, in *krm.ClusterCont
return nil
}
out := &api.ContinuousBackupConfig{
Enabled: ValueOf(in.Enabled),
Enabled: direct.ValueOf(in.Enabled),
EncryptionConfig: EncryptionConfig_KRMToApi(ctx, in.EncryptionConfig),
RecoveryWindowDays: int64(ValueOf(in.RecoveryWindowDays)),
RecoveryWindowDays: int64(direct.ValueOf(in.RecoveryWindowDays)),
}

return out
Expand All @@ -152,10 +152,10 @@ func InitialUser_KRMToApi(ctx *direct.MapContext, in *krm.ClusterInitialUser) *a
return nil
}
out := &api.UserPassword{
User: ValueOf(in.User),
User: direct.ValueOf(in.User),
}
if in.Password.Value != nil {
out.Password = ValueOf(in.Password.Value)
out.Password = direct.ValueOf(in.Password.Value)
} else {
out.Password = in.Password.ValueFrom.SecretKeyRef.Key
}
Expand Down
66 changes: 0 additions & 66 deletions pkg/controller/direct/alloydb/utils.go

This file was deleted.

7 changes: 4 additions & 3 deletions pkg/controller/direct/apikeys/apikeyskey_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import (

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

Expand Down Expand Up @@ -129,7 +130,7 @@ func (m *model) AdapterForObject(ctx context.Context, reader client.Reader, u *u
}

// TODO: Use name or request resourceID to be set on create?
keyID := ValueOf(obj.Spec.ResourceID)
keyID := direct.ValueOf(obj.Spec.ResourceID)
if keyID == "" {
return nil, fmt.Errorf("unable to determine resourceID")
}
Expand Down Expand Up @@ -162,7 +163,7 @@ func (a *adapter) Find(ctx context.Context) (bool, error) {
}
key, err := a.gcp.GetKey(ctx, req)
if err != nil {
if IsNotFound(err) {
if direct.IsNotFound(err) {
klog.Warningf("key was not found: %v", err)
return false, nil
}
Expand All @@ -186,7 +187,7 @@ func (a *adapter) Delete(ctx context.Context) (bool, error) {
}
op, err := a.gcp.DeleteKey(ctx, req)
if err != nil {
if IsNotFound(err) {
if direct.IsNotFound(err) {
return false, nil
}
return false, fmt.Errorf("deleting key: %w", err)
Expand Down
51 changes: 0 additions & 51 deletions pkg/controller/direct/apikeys/utils.go

This file was deleted.

10 changes: 10 additions & 0 deletions pkg/controller/direct/gkehub/acmconversion.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
package gkehub

import (
"strconv"

krm "github.com/GoogleCloudPlatform/k8s-config-connector/apis/gkehub/v1beta1"
featureapi "google.golang.org/api/gkehub/v1beta"
)
Expand Down Expand Up @@ -187,3 +189,11 @@ func convertKRMtoAPI_Oci(r *krm.FeaturemembershipOci) (*featureapi.ConfigManagem
}
return &apiObj, nil
}

func convertStringToInt64(s string) (int64, error) {
val, err := strconv.ParseInt(s, 10, 64)
if err != nil {
return 0, err
}
return val, nil
}
3 changes: 2 additions & 1 deletion pkg/controller/direct/gkehub/featuremembership_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import (
krm "github.com/GoogleCloudPlatform/k8s-config-connector/apis/gkehub/v1beta1"
refs "github.com/GoogleCloudPlatform/k8s-config-connector/apis/refs/v1beta1"
"github.com/GoogleCloudPlatform/k8s-config-connector/pkg/config"
"github.com/GoogleCloudPlatform/k8s-config-connector/pkg/controller/direct"
"github.com/GoogleCloudPlatform/k8s-config-connector/pkg/controller/direct/directbase"
"github.com/GoogleCloudPlatform/k8s-config-connector/pkg/controller/direct/registry"
)
Expand Down Expand Up @@ -146,7 +147,7 @@ func (a *gkeHubAdapter) Find(ctx context.Context) (bool, error) {
}
feature, err := a.featureClient.Get(a.featureID).Context(ctx).Do()
if err != nil {
if IsNotFound(err) {
if direct.IsNotFound(err) {
return false, nil
}
return false, fmt.Errorf("getting feature %q: %w", a.featureID, err)
Expand Down
5 changes: 3 additions & 2 deletions pkg/controller/direct/gkehub/pococonversion.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ package gkehub

import (
krm "github.com/GoogleCloudPlatform/k8s-config-connector/apis/gkehub/v1beta1"
"github.com/GoogleCloudPlatform/k8s-config-connector/pkg/controller/direct"
featureapi "google.golang.org/api/gkehub/v1beta"
)

Expand All @@ -32,10 +33,10 @@ func convertKRMtoAPI_Policycontroller(r *krm.FeaturemembershipPolicycontroller)
func convertKRMtoAPI_PolicycontrollerHubConfig(r krm.FeaturemembershipPolicyControllerHubConfig) *featureapi.PolicyControllerHubConfig {
apiObj := &featureapi.PolicyControllerHubConfig{}
if r.AuditIntervalSeconds != nil {
apiObj.AuditIntervalSeconds = int64(ValueOf(r.AuditIntervalSeconds))
apiObj.AuditIntervalSeconds = int64(direct.ValueOf(r.AuditIntervalSeconds))
}
if r.ConstraintViolationLimit != nil {
apiObj.ConstraintViolationLimit = int64(ValueOf(r.ConstraintViolationLimit))
apiObj.ConstraintViolationLimit = int64(direct.ValueOf(r.ConstraintViolationLimit))
}
if r.ExemptableNamespaces != nil {
apiObj.ExemptableNamespaces = r.ExemptableNamespaces
Expand Down
3 changes: 2 additions & 1 deletion pkg/controller/direct/gkehub/references.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"strings"

krm "github.com/GoogleCloudPlatform/k8s-config-connector/apis/gkehub/v1beta1"
"github.com/GoogleCloudPlatform/k8s-config-connector/pkg/controller/direct"
apierrors "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime/schema"
Expand Down Expand Up @@ -85,7 +86,7 @@ func resolveMembershipRef(ctx context.Context, reader client.Reader, obj *krm.GK
if membershipName == "" {
membershipName = membership.GetName()
}
membershipLocation := ValueOf(obj.Spec.MembershipLocation)
membershipLocation := direct.ValueOf(obj.Spec.MembershipLocation)
if membershipLocation == "" {
// membership location should default to global if not set.
membershipLocation = "global"
Expand Down
Loading

0 comments on commit 80cd23e

Please sign in to comment.