Skip to content

Commit

Permalink
Merge pull request GoogleCloudPlatform#2238 from yuwenma/cbwp-dynamic…
Browse files Browse the repository at this point in the history
…test

feat: enable pure direct resource on dynamic test
  • Loading branch information
google-oss-prow[bot] authored Jul 10, 2024
2 parents 902d75b + eb9f53d commit 1ce0259
Show file tree
Hide file tree
Showing 3 changed files with 94 additions and 65 deletions.
2 changes: 2 additions & 0 deletions pkg/controller/dynamic/dynamic_controller_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@ import (
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/log"
"sigs.k8s.io/controller-runtime/pkg/manager"

_ "github.com/GoogleCloudPlatform/k8s-config-connector/pkg/controller/direct/register"
)

type httpRoundTripperKeyType int
Expand Down
9 changes: 9 additions & 0 deletions pkg/krmtotf/resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,15 @@ func NewResource(u *unstructured.Unstructured, sm *corekccv1alpha1.ServiceMappin

func NewResourceFromResourceConfig(rc *corekccv1alpha1.ResourceConfig, p *tfschema.Provider) (*Resource, error) {
tfResource, ok := p.ResourcesMap[rc.Name]
// Pure Direct Resource does not have ResourceMap.
if rc.Direct {
return &Resource{
TFInfo: &terraform.InstanceInfo{
Type: rc.Name,
},
ResourceConfig: *rc,
}, nil
}
if !ok {
return nil, fmt.Errorf("error getting TF resource: unknown resource %v", rc.Name)
}
Expand Down
148 changes: 83 additions & 65 deletions pkg/test/resourcefixture/contexts/register.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,10 @@ import (
"testing"

"github.com/GoogleCloudPlatform/k8s-config-connector/pkg/apis/core/v1alpha1"
"github.com/GoogleCloudPlatform/k8s-config-connector/pkg/config"
dclcontroller "github.com/GoogleCloudPlatform/k8s-config-connector/pkg/controller/dcl"
"github.com/GoogleCloudPlatform/k8s-config-connector/pkg/controller/direct/directbase"
"github.com/GoogleCloudPlatform/k8s-config-connector/pkg/controller/direct/registry"

"github.com/GoogleCloudPlatform/k8s-config-connector/pkg/controller/direct/logging"
"github.com/GoogleCloudPlatform/k8s-config-connector/pkg/dcl"
dclconversion "github.com/GoogleCloudPlatform/k8s-config-connector/pkg/dcl/conversion"
dclextension "github.com/GoogleCloudPlatform/k8s-config-connector/pkg/dcl/extension"
Expand Down Expand Up @@ -112,14 +112,6 @@ func (rc ResourceContext) getResourceConfig(smLoader *servicemappingloader.Servi
panic(fmt.Errorf("no resource config found for kind: %v", rc.ResourceKind))
}

func (rc ResourceContext) IsDirectResource() bool {
switch rc.ResourceGVK.GroupKind() {
case schema.GroupKind{Group: "logging.cnrm.cloud.google.com", Kind: "LoggingLogMetric"}:
return true
}
return false
}

func (rc ResourceContext) IsAutoGenerated(smLoader *servicemappingloader.ServiceMappingLoader) bool {
if rc.DCLBased {
return false
Expand All @@ -129,40 +121,18 @@ func (rc ResourceContext) IsAutoGenerated(smLoader *servicemappingloader.Service
}

func (rc ResourceContext) Create(ctx context.Context, _ *testing.T, u *unstructured.Unstructured, provider *tfschema.Provider, c client.Client, smLoader *servicemappingloader.ServiceMappingLoader, config *mmdcl.Config, dclConverter *dclconversion.Converter) (*unstructured.Unstructured, error) {
if rc.IsDirectResource() {
return directCreate(ctx, u, c)
}
if rc.DCLBased {
return dclCreate(ctx, u, config, c, dclConverter, smLoader)
}
return terraformCreate(ctx, u, provider, c, smLoader)
}

func (rc ResourceContext) Get(ctx context.Context, _ *testing.T, u *unstructured.Unstructured, provider *tfschema.Provider, c client.Client, smLoader *servicemappingloader.ServiceMappingLoader, cfg *mmdcl.Config, dclConverter *dclconversion.Converter, httpClient *http.Client) (*unstructured.Unstructured, error) {
// direct controllers
switch u.GroupVersionKind().GroupKind() {
case schema.GroupKind{Group: "logging.cnrm.cloud.google.com", Kind: "LoggingLogMetric"}:
m, err := logging.NewLogMetricModel(ctx, &config.ControllerConfig{
HTTPClient: httpClient,
})
if err != nil {
return nil, err
}
a, err := m.AdapterForObject(ctx, c, u)
if err != nil {
return nil, err
}
found, err := a.Find(ctx)
if err != nil {
return nil, err
}
if !found {
return nil, fmt.Errorf("logging.cnrm.cloud.google.com/LoggingLogMetric '%v' is not found", u.GetName())
}

unst, err := a.Export(ctx)
if err != nil {
return nil, err
}

return unst, nil
if rc.IsDirectResource() {
return directExport(ctx, u, c)
}

if rc.DCLBased {
Expand All @@ -172,34 +142,8 @@ func (rc ResourceContext) Get(ctx context.Context, _ *testing.T, u *unstructured
}

func (rc ResourceContext) Delete(ctx context.Context, _ *testing.T, u *unstructured.Unstructured, provider *tfschema.Provider, c client.Client, smLoader *servicemappingloader.ServiceMappingLoader, cfg *mmdcl.Config, dclConverter *dclconversion.Converter, httpClient *http.Client) error {
// direct controllers
switch u.GroupVersionKind().GroupKind() {
case schema.GroupKind{Group: "logging.cnrm.cloud.google.com", Kind: "LoggingLogMetric"}:
m, err := logging.NewLogMetricModel(ctx, &config.ControllerConfig{
HTTPClient: httpClient,
})
if err != nil {
return err
}
a, err := m.AdapterForObject(ctx, c, u)
if err != nil {
return err
}
found, err := a.Find(ctx)
if err != nil {
return err
}
if !found {
return fmt.Errorf("logging.cnrm.cloud.google.com/LoggingLogMetric '%v' not found", u.GetName())
}

deleted, err := a.Delete(ctx)
if err != nil {
return err
}
if !deleted {
return fmt.Errorf("did not delete logging.cnrm.cloud.google.com/LoggingLogMetric '%v'", u.GetName())
}
if rc.IsDirectResource() {
return directDelete(ctx, u, c)
}
if rc.DCLBased {
return dclDelete(ctx, u, cfg, c, dclConverter, smLoader)
Expand Down Expand Up @@ -390,3 +334,77 @@ func IsNotFoundError(err error) bool {
}
return strings.Contains(err.Error(), "is not found")
}

func (rc ResourceContext) IsDirectResource() bool {
gk := rc.ResourceGVK.GroupKind()
return registry.IsDirectByGK(gk)
}

func getAdapter(ctx context.Context, u *unstructured.Unstructured, c client.Client) (directbase.Adapter, error) {
gvk := u.GroupVersionKind()
model, err := registry.GetModel(gvk.GroupKind())
if err != nil {
return nil, err
}
return model.AdapterForObject(ctx, c, u)
}

func directExport(ctx context.Context, u *unstructured.Unstructured, c client.Client) (*unstructured.Unstructured, error) {
a, err := getAdapter(ctx, u, c)
if err != nil {
return nil, err
}

found, err := a.Find(ctx)
if err != nil {
return nil, err
}
if !found {
return nil, fmt.Errorf("GVK %s '%v' is not found", u.GroupVersionKind(), u.GetName())
}

unst, err := a.Export(ctx)
if err != nil {
return nil, err
}
return unst, nil
}

func directCreate(ctx context.Context, u *unstructured.Unstructured, c client.Client) (*unstructured.Unstructured, error) {
a, err := getAdapter(ctx, u, c)
if err != nil {
return nil, err
}

found, err := a.Find(ctx)
if err != nil {
return nil, err
}
if found {
return nil, fmt.Errorf("GVK %s '%v' already exist", u.GroupVersionKind(), u.GetName())
}

err = a.Create(ctx, u)
if err != nil {
return nil, err
}
return directExport(ctx, u, c)
}

func directDelete(ctx context.Context, u *unstructured.Unstructured, c client.Client) error {
a, err := getAdapter(ctx, u, c)
if err != nil {
return err
}

found, err := a.Find(ctx)
if err != nil {
return err
}
if !found {
return fmt.Errorf("GVK %s '%v' is not found", u.GroupVersionKind(), u.GetName())
}

_, err = a.Delete(ctx)
return err
}

0 comments on commit 1ce0259

Please sign in to comment.