Skip to content

Commit

Permalink
Update CustomResourceDefinitions from v1beta1 to v1
Browse files Browse the repository at this point in the history
  • Loading branch information
nickschuch committed Mar 7, 2023
1 parent 632cc52 commit c0175cc
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 19 deletions.
2 changes: 1 addition & 1 deletion internal/resources/apiextensions/v1beta1/crd/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ func Create(d *schema.ResourceData, m interface{}) error {
return errors.Wrap(err, "failed to generate")
}

out, err := conn.APIExtensions().ApiextensionsV1beta1().CustomResourceDefinitions().Create(&crd)
out, err := conn.APIExtensions().ApiextensionsV1().CustomResourceDefinitions().Create(&crd)
if err != nil {
return errors.Wrap(err, "failed to create")
}
Expand Down
26 changes: 16 additions & 10 deletions internal/resources/apiextensions/v1beta1/crd/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@ package crd

import (
"github.com/hashicorp/terraform/helper/schema"
apiextensionsv1beta1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1beta1"
apiextensionsv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"

"github.com/previousnext/terraform-provider-k8s/internal/interfaceutils"
"github.com/previousnext/terraform-provider-k8s/internal/resources/apiextensions/v1beta1/crd/names"
)

// Generate the ServiceAccount.
func Generate(d *schema.ResourceData) (apiextensionsv1beta1.CustomResourceDefinition, error) {
func Generate(d *schema.ResourceData) (apiextensionsv1.CustomResourceDefinition, error) {
var (
name = d.Get(FieldName).(string)
rawLabels = d.Get(FieldLabels).(map[string]interface{})
Expand All @@ -20,19 +20,25 @@ func Generate(d *schema.ResourceData) (apiextensionsv1beta1.CustomResourceDefini
rawNames = d.Get(FieldNames).([]interface{})
)

crd := apiextensionsv1beta1.CustomResourceDefinition{
crd := apiextensionsv1.CustomResourceDefinition{
ObjectMeta: metav1.ObjectMeta{
Name: name,
Labels: interfaceutils.ExpandMap(rawLabels),
},
Spec: apiextensionsv1beta1.CustomResourceDefinitionSpec{
Group: group,
Version: version,
Scope: apiextensionsv1beta1.ResourceScope(scope),
Names: names.Expand(rawNames),
Subresources: &apiextensionsv1beta1.CustomResourceSubresources{
Status: &apiextensionsv1beta1.CustomResourceSubresourceStatus{},
Spec: apiextensionsv1.CustomResourceDefinitionSpec{
Group: group,
Versions: []apiextensionsv1.CustomResourceDefinitionVersion{
{
Name: version,
Served: true,
Storage: true,
Subresources: &apiextensionsv1.CustomResourceSubresources{
Status: &apiextensionsv1.CustomResourceSubresourceStatus{},
},
},
},
Scope: apiextensionsv1.ResourceScope(scope),
Names: names.Expand(rawNames),
},
}

Expand Down
6 changes: 3 additions & 3 deletions internal/resources/apiextensions/v1beta1/crd/names/expand.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
package names

import (
apiextensionsv1beta1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1beta1"
apiextensionsv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"

"github.com/previousnext/terraform-provider-k8s/internal/interfaceutils"
)

// Expand will return a structured object.
func Expand(in []interface{}) apiextensionsv1beta1.CustomResourceDefinitionNames {
var names apiextensionsv1beta1.CustomResourceDefinitionNames
func Expand(in []interface{}) apiextensionsv1.CustomResourceDefinitionNames {
var names apiextensionsv1.CustomResourceDefinitionNames

if len(in) == 0 {
return names
Expand Down
4 changes: 2 additions & 2 deletions internal/resources/apiextensions/v1beta1/crd/names/flatten.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
package names

import (
apiextensionsv1beta1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1beta1"
apiextensionsv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
)

// Flatten structured object into unstructured.
func Flatten(in apiextensionsv1beta1.CustomResourceDefinitionNames) []interface{} {
func Flatten(in apiextensionsv1.CustomResourceDefinitionNames) []interface{} {
out := make([]interface{}, 1)

row := map[string]interface{}{}
Expand Down
8 changes: 6 additions & 2 deletions internal/resources/apiextensions/v1beta1/crd/read.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ func Read(d *schema.ResourceData, m interface{}) error {
return errors.Wrap(err, "failed to get ID")
}

crd, err := conn.APIExtensions().ApiextensionsV1beta1().CustomResourceDefinitions().Get(name, metav1.GetOptions{})
crd, err := conn.APIExtensions().ApiextensionsV1().CustomResourceDefinitions().Get(name, metav1.GetOptions{})
if kerrors.IsNotFound(err) {
// This is how we tell Terraform that the resource does not exist.
d.SetId("")
Expand All @@ -34,7 +34,11 @@ func Read(d *schema.ResourceData, m interface{}) error {
d.Set(FieldAnnotations, crd.ObjectMeta.Annotations)

d.Set(FieldGroup, crd.Spec.Group)
d.Set(FieldVersion, crd.Spec.Version)

if len(crd.Spec.Versions) > 0 {
d.Set(FieldVersion, crd.Spec.Versions[0].Name)
}

d.Set(FieldScope, crd.Spec.Scope)
d.Set(FieldNames, names.Flatten(crd.Spec.Names))

Expand Down
3 changes: 2 additions & 1 deletion internal/resources/apiextensions/v1beta1/crd/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package crd
import (
"github.com/hashicorp/terraform/helper/schema"
"github.com/pkg/errors"

"github.com/previousnext/terraform-provider-k8s/internal/terraform/config"
)

Expand All @@ -15,7 +16,7 @@ func Update(d *schema.ResourceData, m interface{}) error {
return errors.Wrap(err, "failed to generate")
}

_, err = conn.APIExtensions().ApiextensionsV1beta1().CustomResourceDefinitions().Update(&crd)
_, err = conn.APIExtensions().ApiextensionsV1().CustomResourceDefinitions().Update(&crd)
if err != nil {
return errors.Wrap(err, "failed to update")
}
Expand Down

0 comments on commit c0175cc

Please sign in to comment.