Skip to content

Commit

Permalink
Replace interface{} usages with any (#2453)
Browse files Browse the repository at this point in the history
Go 1.18 introduced an alias for `interface{}` called `any` that is shorter and more descriptive. Replace all instances of `interface{}` in the provider and test code with `any`.

These changes were created using the following command:
`gofmt -w -r 'interface{} -> any' .`
  • Loading branch information
lblackstone committed Jun 9, 2023
1 parent b5ac199 commit 891740e
Show file tree
Hide file tree
Showing 49 changed files with 559 additions and 559 deletions.
8 changes: 4 additions & 4 deletions provider/cmd/pulumi-gen-kubernetes/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ func generateSchema(swaggerPath string) schema.PackageSpec {
swagger = mergeSwaggerSpecs(legacySwagger, swagger)
}

var schemaMap map[string]interface{}
var schemaMap map[string]any
err = json.Unmarshal(swagger, &schemaMap)
if err != nil {
panic(err)
Expand Down Expand Up @@ -425,7 +425,7 @@ func mustLoadGoFile(path string) []byte {
return formattedSource
}

func mustRenderTemplate(path string, resources interface{}) []byte {
func mustRenderTemplate(path string, resources any) []byte {
b := mustLoadFile(path)
t := template.Must(template.New("resources").Parse(string(b)))

Expand All @@ -437,7 +437,7 @@ func mustRenderTemplate(path string, resources interface{}) []byte {
return buf.Bytes()
}

func mustRenderGoTemplate(path string, resources interface{}) []byte {
func mustRenderGoTemplate(path string, resources any) []byte {
bytes := mustRenderTemplate(path, resources)

formattedSource, err := format.Source(bytes)
Expand Down Expand Up @@ -497,7 +497,7 @@ func mustWriteFile(rootDir, filename string, contents []byte) {
}
}

func makeJSONString(v interface{}) ([]byte, error) {
func makeJSONString(v any) ([]byte, error) {
var out bytes.Buffer
encoder := json.NewEncoder(&out)
encoder.SetEscapeHTML(false)
Expand Down
2 changes: 1 addition & 1 deletion provider/cmd/pulumi-gen-kubernetes/merge.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import (

func mergeSwaggerSpecs(legacyBytes, currentBytes []byte) []byte {

var legacyObj, newObj map[string]interface{}
var legacyObj, newObj map[string]any
err := json.Unmarshal(legacyBytes, &legacyObj)
if err != nil {
panic(err)
Expand Down
2 changes: 1 addition & 1 deletion provider/pkg/await/await_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ func (mri *mockResourceInterface) DeleteCollection(
func (mri *mockResourceInterface) Get(
ctx context.Context, name string, options metav1.GetOptions, subresources ...string,
) (*unstructured.Unstructured, error) {
return &unstructured.Unstructured{Object: map[string]interface{}{}}, nil
return &unstructured.Unstructured{Object: map[string]any{}}, nil
}
func (mri *mockResourceInterface) List(ctx context.Context, opts metav1.ListOptions) (*unstructured.UnstructuredList, error) {
panic("List not implemented")
Expand Down
28 changes: 14 additions & 14 deletions provider/pkg/await/awaiters.go
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,7 @@ var awaiters = map[string]awaitSpec{

// --------------------------------------------------------------------------

func deploymentSpecReplicas(deployment *unstructured.Unstructured) (interface{}, bool) {
func deploymentSpecReplicas(deployment *unstructured.Unstructured) (any, bool) {
return openapi.Pluck(deployment.Object, "spec", "replicas")
}

Expand All @@ -285,7 +285,7 @@ func untilAppsDeploymentDeleted(config deleteAwaitConfig) error {
// transient network partition (or something) that it could be successfully deleted and GC'd
// before we get to check it, which I think would require manual intervention.
//
statusReplicas := func(deployment *unstructured.Unstructured) (interface{}, bool) {
statusReplicas := func(deployment *unstructured.Unstructured) (any, bool) {
return openapi.Pluck(deployment.Object, "status", "replicas")
}

Expand Down Expand Up @@ -325,10 +325,10 @@ func untilAppsDeploymentDeleted(config deleteAwaitConfig) error {
// --------------------------------------------------------------------------

func untilAppsStatefulSetDeleted(config deleteAwaitConfig) error {
specReplicas := func(statefulset *unstructured.Unstructured) (interface{}, bool) {
specReplicas := func(statefulset *unstructured.Unstructured) (any, bool) {
return openapi.Pluck(statefulset.Object, "spec", "replicas")
}
statusReplicas := func(statefulset *unstructured.Unstructured) (interface{}, bool) {
statusReplicas := func(statefulset *unstructured.Unstructured) (any, bool) {
return openapi.Pluck(statefulset.Object, "status", "replicas")
}

Expand Down Expand Up @@ -494,12 +494,12 @@ func untilCoreV1PodDeleted(config deleteAwaitConfig) error {

// --------------------------------------------------------------------------

func replicationControllerSpecReplicas(rc *unstructured.Unstructured) (interface{}, bool) {
func replicationControllerSpecReplicas(rc *unstructured.Unstructured) (any, bool) {
return openapi.Pluck(rc.Object, "spec", "replicas")
}

func untilCoreV1ReplicationControllerInitialized(c createAwaitConfig) error {
availableReplicas := func(rc *unstructured.Unstructured) (interface{}, bool) {
availableReplicas := func(rc *unstructured.Unstructured) (any, bool) {
return openapi.Pluck(rc.Object, "status", "availableReplicas")
}

Expand Down Expand Up @@ -542,7 +542,7 @@ func untilCoreV1ReplicationControllerDeleted(config deleteAwaitConfig) error {
// transient network partition (or something) that it could be successfully deleted and GC'd
// before we get to check it, which I think would require manual intervention.
//
statusReplicas := func(rc *unstructured.Unstructured) (interface{}, bool) {
statusReplicas := func(rc *unstructured.Unstructured) (any, bool) {
return openapi.Pluck(rc.Object, "status", "replicas")
}

Expand Down Expand Up @@ -586,8 +586,8 @@ func untilCoreV1ResourceQuotaInitialized(c createAwaitConfig) error {
hardRaw, _ := openapi.Pluck(quota.Object, "spec", "hard")
hardStatusRaw, _ := openapi.Pluck(quota.Object, "status", "hard")

hard, hardIsMap := hardRaw.(map[string]interface{})
hardStatus, hardStatusIsMap := hardStatusRaw.(map[string]interface{})
hard, hardIsMap := hardRaw.(map[string]any)
hardStatus, hardStatusIsMap := hardStatusRaw.(map[string]any)
if hardIsMap && hardStatusIsMap && reflect.DeepEqual(hard, hardStatus) {
logger.V(3).Infof("ResourceQuota %q initialized: %#v", c.currentInputs.GetName(),
c.currentInputs)
Expand Down Expand Up @@ -636,7 +636,7 @@ func untilCoreV1SecretInitialized(c createAwaitConfig) error {

secretDataAllocated := func(secret *unstructured.Unstructured) bool {
data, _ := openapi.Pluck(secret.Object, "data")
if secretData, isMap := data.(map[string]interface{}); isMap {
if secretData, isMap := data.(map[string]any); isMap {
// We don't need to wait for any specific initialization. Most of the time we create a secret with
// empty data which are propagated by controller so it's enough to check if data map is not empty.
return len(secretData) > 0
Expand Down Expand Up @@ -674,7 +674,7 @@ func untilCoreV1ServiceAccountInitialized(c createAwaitConfig) error {

specSecrets, _ := openapi.Pluck(c.currentInputs.Object, "secrets")
var numSpecSecrets int
if specSecretsArr, isArr := specSecrets.([]interface{}); isArr {
if specSecretsArr, isArr := specSecrets.([]any); isArr {
numSpecSecrets = len(specSecretsArr)
} else {
numSpecSecrets = 0
Expand All @@ -683,7 +683,7 @@ func untilCoreV1ServiceAccountInitialized(c createAwaitConfig) error {
defaultSecretAllocated := func(sa *unstructured.Unstructured) bool {
secrets, _ := openapi.Pluck(sa.Object, "secrets")
logger.V(3).Infof("ServiceAccount %q contains secrets: %#v", sa.GetName(), secrets)
if secretsArr, isArr := secrets.([]interface{}); isArr {
if secretsArr, isArr := secrets.([]any); isArr {
numSecrets := len(secretsArr)
logger.V(3).Infof("ServiceAccount %q has allocated '%d' of '%d' secrets",
sa.GetName(), numSecrets, numSpecSecrets+1)
Expand All @@ -710,8 +710,8 @@ func untilCoreV1ServiceAccountInitialized(c createAwaitConfig) error {
// it until the desired replicas are the same as the current replicas. The user provides two
// functions to obtain the replicas spec and status fields, as well as a client to access them.
func waitForDesiredReplicasFunc(
getReplicasSpec func(*unstructured.Unstructured) (interface{}, bool),
getReplicasStatus func(*unstructured.Unstructured) (interface{}, bool),
getReplicasSpec func(*unstructured.Unstructured) (any, bool),
getReplicasStatus func(*unstructured.Unstructured) (any, bool),
) watcher.Predicate {
return func(replicator *unstructured.Unstructured) bool {
desiredReplicas, hasReplicasSpec := getReplicasSpec(replicator)
Expand Down
12 changes: 6 additions & 6 deletions provider/pkg/await/deployment.go
Original file line number Diff line number Diff line change
Expand Up @@ -472,7 +472,7 @@ func (dia *deploymentInitAwaiter) processDeploymentEvent(event watch.Event) {
// Check Deployments conditions to see whether new ReplicaSet is available. If it is, we are
// successful.
rawConditions, hasConditions := openapi.Pluck(deployment.Object, "status", "conditions")
conditions, isSlice := rawConditions.([]interface{})
conditions, isSlice := rawConditions.([]any)
if !hasConditions || !isSlice {
// Deployment controller has not made progress yet. Do nothing.
return
Expand All @@ -481,7 +481,7 @@ func (dia *deploymentInitAwaiter) processDeploymentEvent(event watch.Event) {
// Success occurs when the ReplicaSet of the `replicaSetGeneration` is marked as available, and
// when the deployment is available.
for _, rawCondition := range conditions {
condition, isMap := rawCondition.(map[string]interface{})
condition, isMap := rawCondition.(map[string]any)
if !isMap {
continue
}
Expand Down Expand Up @@ -592,7 +592,7 @@ func (dia *deploymentInitAwaiter) checkReplicaSetStatus() {
specReplicas = 1
}

var rawReadyReplicas interface{}
var rawReadyReplicas any
var readyReplicas int64
var readyReplicasExists bool
var unavailableReplicas int64
Expand Down Expand Up @@ -775,11 +775,11 @@ func (dia *deploymentInitAwaiter) processPersistentVolumeClaimsEvent(event watch
// Check any PersistentVolumeClaims that the Deployments Volumes may have
// by name against the PersistentVolumeClaim in the event
volumes, _ := openapi.Pluck(dia.deployment.Object, "spec", "template", "spec", "volumes")
vols, _ := volumes.([]interface{})
vols, _ := volumes.([]any)
for _, vol := range vols {
v := vol.(map[string]interface{})
v := vol.(map[string]any)
if deployPVC, exists := v["persistentVolumeClaim"]; exists {
p := deployPVC.(map[string]interface{})
p := deployPVC.(map[string]any)
claimName := p["claimName"].(string)

if claimName == pvc.GetName() {
Expand Down
6 changes: 3 additions & 3 deletions provider/pkg/await/informers/informer.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,19 +102,19 @@ func New(

if options.informChan != nil {
_, err := informer.Informer().AddEventHandler(cache.ResourceEventHandlerFuncs{
AddFunc: func(obj interface{}) {
AddFunc: func(obj any) {
options.informChan <- watch.Event{
Object: obj.(*unstructured.Unstructured),
Type: watch.Added,
}
},
UpdateFunc: func(_, newObj interface{}) {
UpdateFunc: func(_, newObj any) {
options.informChan <- watch.Event{
Object: newObj.(*unstructured.Unstructured),
Type: watch.Modified,
}
},
DeleteFunc: func(obj interface{}) {
DeleteFunc: func(obj any) {
if unknown, ok := obj.(cache.DeletedFinalStateUnknown); ok {
options.informChan <- watch.Event{
Object: unknown.Obj.(*unstructured.Unstructured),
Expand Down
4 changes: 2 additions & 2 deletions provider/pkg/await/ingress.go
Original file line number Diff line number Diff line change
Expand Up @@ -319,7 +319,7 @@ func (iia *ingressInitAwaiter) processIngressEvent(event watch.Event) {
return
}

ingresses, ok := ingressesRaw.([]interface{})
ingresses, ok := ingressesRaw.([]any)
if !ok {
logger.V(3).Infof("Unexpected ingress object structure from unstructured: %#v", ingress)
return
Expand All @@ -332,7 +332,7 @@ func (iia *ingressInitAwaiter) processIngressEvent(event watch.Event) {
inputIngressName)
}

func decodeIngress(u *unstructured.Unstructured, to interface{}) error {
func decodeIngress(u *unstructured.Unstructured, to any) error {
b, err := u.MarshalJSON()
if err != nil {
return err
Expand Down
8 changes: 4 additions & 4 deletions provider/pkg/await/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -303,7 +303,7 @@ func (sia *serviceInitAwaiter) processServiceEvent(event watch.Event) {
status, _ := openapi.Pluck(service.Object, "status")

logger.V(3).Infof("Received status for service %q: %#v", inputServiceName, status)
ing, isSlice := lbIngress.([]interface{})
ing, isSlice := lbIngress.([]any)

// Update status of service object so that we can check success.
sia.serviceReady = isSlice && len(ing) > 0
Expand Down Expand Up @@ -341,7 +341,7 @@ func (sia *serviceInitAwaiter) processEndpointEvent(event watch.Event, settledCh
// Update status of endpoint objects so we can check success.
if event.Type == watch.Added || event.Type == watch.Modified {
subsets, hasTargets := openapi.Pluck(endpoint.Object, "subsets")
targets, targetsIsSlice := subsets.([]interface{})
targets, targetsIsSlice := subsets.([]any)
endpointTargetsPod := hasTargets && targetsIsSlice && len(targets) > 0

sia.endpointsReady = endpointTargetsPod
Expand Down Expand Up @@ -397,7 +397,7 @@ func (sia *serviceInitAwaiter) isExternalNameService() bool {
// [1]: https://kubernetes.io/docs/concepts/services-networking/service/#headless-services
func (sia *serviceInitAwaiter) emptyHeadlessOrExternalName() bool {
selectorI, _ := openapi.Pluck(sia.service.Object, "spec", "selector")
selector, _ := selectorI.(map[string]interface{})
selector, _ := selectorI.(map[string]any)

headlessEmpty := len(selector) == 0 && sia.isHeadlessService()
return headlessEmpty || sia.isExternalNameService()
Expand All @@ -419,7 +419,7 @@ func (sia *serviceInitAwaiter) hasHeadlessServicePortBug(version cluster.ServerV
// k8s versions < 1.12 have the bug.
if version.Compare(cluster.ServerVersion{Major: 1, Minor: 12}) < 0 {
portsI, _ := openapi.Pluck(sia.service.Object, "spec", "ports")
ports, _ := portsI.([]map[string]interface{})
ports, _ := portsI.([]map[string]any)
hasPorts := len(ports) > 0

// The bug affects Services with no specified ports.
Expand Down
38 changes: 19 additions & 19 deletions provider/pkg/clients/unstructured_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,14 +44,14 @@ var (
}

validPodUnstructured = &unstructured.Unstructured{
Object: map[string]interface{}{
Object: map[string]any{
"apiVersion": "v1",
"kind": "Pod",
"metadata": map[string]interface{}{
"metadata": map[string]any{
"name": "foo"},
"spec": map[string]interface{}{
"containers": []interface{}{
map[string]interface{}{
"spec": map[string]any{
"containers": []any{
map[string]any{
"name": "foo",
"image": "nginx"}},
},
Expand Down Expand Up @@ -81,16 +81,16 @@ var (
}

validDeploymentUnstructured = &unstructured.Unstructured{
Object: map[string]interface{}{
Object: map[string]any{
"apiVersion": "apps/v1",
"kind": "Deployment",
"metadata": map[string]interface{}{
"metadata": map[string]any{
"name": "foo"},
"spec": map[string]interface{}{
"template": map[string]interface{}{
"spec": map[string]interface{}{
"containers": []interface{}{
map[string]interface{}{
"spec": map[string]any{
"template": map[string]any{
"spec": map[string]any{
"containers": []any{
map[string]any{
"name": "foo",
"image": "nginx"}},
},
Expand All @@ -101,16 +101,16 @@ var (

// Unregistered GVK
unregisteredGVK = &unstructured.Unstructured{
Object: map[string]interface{}{
Object: map[string]any{
"apiVersion": "pulumi/test",
"kind": "Foo",
"metadata": map[string]interface{}{
"metadata": map[string]any{
"name": "foo"},
"spec": map[string]interface{}{
"template": map[string]interface{}{
"spec": map[string]interface{}{
"containers": []interface{}{
map[string]interface{}{
"spec": map[string]any{
"template": map[string]any{
"spec": map[string]any{
"containers": []any{
map[string]any{
"name": "foo",
"image": "nginx"}},
},
Expand Down
2 changes: 1 addition & 1 deletion provider/pkg/gen/examples/upstream/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ func processYaml(path string, mdDir string) error {
decoder := yaml.NewDecoder(yamlFile)
exampleStrings := []string{}
for {
example := map[string]interface{}{}
example := map[string]any{}
err := decoder.Decode(&example)
if err == io.EOF {
break
Expand Down
Loading

0 comments on commit 891740e

Please sign in to comment.