From 9de61961daa394278aa899f8adf36e626623e24e Mon Sep 17 00:00:00 2001 From: Alex Pana <8968914+acpana@users.noreply.github.com> Date: Wed, 24 Jul 2024 20:45:19 +0000 Subject: [PATCH 1/2] enh: use maputils Signed-off-by: Alex Pana <8968914+acpana@users.noreply.github.com> --- .../controllerbuilder/template/controller.go | 45 ++----------------- 1 file changed, 3 insertions(+), 42 deletions(-) diff --git a/dev/tools/controllerbuilder/template/controller.go b/dev/tools/controllerbuilder/template/controller.go index 0bb18f973f..e53cbffdfd 100644 --- a/dev/tools/controllerbuilder/template/controller.go +++ b/dev/tools/controllerbuilder/template/controller.go @@ -33,6 +33,7 @@ import ( krm "github.com/GoogleCloudPlatform/k8s-config-connector/apis/{{.Service}}/{{.Version}}" 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" @@ -93,7 +94,7 @@ func (m *model) AdapterForObject(ctx context.Context, reader client.Reader, u *u } // Get ResourceID - resourceID := ValueOf(obj.Spec.ResourceID) + resourceID := direct.ValueOf(obj.Spec.ResourceID) if resourceID == "" { resourceID = obj.GetName() } @@ -151,7 +152,7 @@ func (a *Adapter) Find(ctx context.Context) (bool, error) { req := &{{.Service}}pb.Get{{.Kind}}Request{Name: a.fullyQualifiedName()} {{.KindToLower}}pb, err := a.gcpClient.Get{{.Kind}}(ctx, req) if err != nil { - if IsNotFound(err) { + if direct.IsNotFound(err) { return false, nil } return false, fmt.Errorf("getting {{.Kind}} %q: %w", a.fullyQualifiedName(), err) @@ -297,46 +298,6 @@ func setStatus(u *unstructured.Unstructured, typedStatus any) error { return nil } -func ValueOf[T any](p *T) T { - var v T - if p != nil { - v = *p - } - return v -} - -// IsNotFound returns true if the given error is an HTTP 404. -func IsNotFound(err error) bool { - return HasHTTPCode(err, 404) -} - -// HasHTTPCode returns true if the given error is an HTTP response with the given code. -func HasHTTPCode(err error, code int) bool { - if err == nil { - return false - } - apiError := &apierror.APIError{} - if errors.As(err, &apiError) { - if apiError.HTTPCode() == code { - return true - } - } else { - klog.Warningf("unexpected error type %T", err) - } - return false -} - -// LazyPtr returns a pointer to v, unless it is the empty value, in which case it returns nil. -// It is essentially the inverse of ValueOf, though it is lossy -// because we can't tell nil and empty apart without a pointer. -func LazyPtr[T comparable](v T) *T { - var defaultValue T - if v == defaultValue { - return nil - } - return &v -} - func ToOpenAPIDateTime(ts *timestamppb.Timestamp) *string { formatted := ts.AsTime().Format(time.RFC3339) return &formatted From 2453de260aa1c9b6a9c7eeabca42b59d14235498 Mon Sep 17 00:00:00 2001 From: Alex Pana <8968914+acpana@users.noreply.github.com> Date: Wed, 24 Jul 2024 21:20:19 +0000 Subject: [PATCH 2/2] enh: use maputils Signed-off-by: Alex Pana <8968914+acpana@users.noreply.github.com> --- dev/tools/proto-to-mapper/main.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/dev/tools/proto-to-mapper/main.go b/dev/tools/proto-to-mapper/main.go index 8246ff2d0a..b48f986772 100644 --- a/dev/tools/proto-to-mapper/main.go +++ b/dev/tools/proto-to-mapper/main.go @@ -362,7 +362,7 @@ func (v *visitor) writeMapFunctionsForPair(out io.Writer, pair *typePair) { } { - fmt.Fprintf(out, "func %s_FromProto(mapCtx *MapContext, in *pb.%s) *krm.%s {\n", goTypeName, pbTypeName, goTypeName) + fmt.Fprintf(out, "func %s_FromProto(mapCtx *direct.MapContext, in *pb.%s) *krm.%s {\n", goTypeName, pbTypeName, goTypeName) fmt.Fprintf(out, "\tif in == nil {\n") fmt.Fprintf(out, "\t\treturn nil\n") fmt.Fprintf(out, "\t}\n") @@ -658,7 +658,7 @@ func (v *visitor) writeMapFunctionsForPair(out io.Writer, pair *typePair) { krmFieldName, ) } else { - fmt.Fprintf(out, "\tout.%s = ValueOf(in.%s)\n", + fmt.Fprintf(out, "\tout.%s = direct.ValueOf(in.%s)\n", protoFieldName, krmFieldName, )