From 2a4e9efffa7c0382dbd396b8ca6a41df36702d63 Mon Sep 17 00:00:00 2001 From: Matt Boersma Date: Wed, 9 Oct 2024 15:29:14 -0600 Subject: [PATCH] Remove unused client code --- azure/scope/machine_test.go | 3 +- azure/services/virtualmachineimages/cache.go | 132 ------------------ .../virtualmachineimages/cache_test.go | 76 ---------- azure/services/virtualmachineimages/client.go | 70 ---------- azure/services/virtualmachineimages/images.go | 25 ++-- .../mock_virtualmachineimages/client_mock.go | 72 ---------- .../mock_virtualmachineimages/doc.go | 21 --- 7 files changed, 15 insertions(+), 384 deletions(-) delete mode 100644 azure/services/virtualmachineimages/cache.go delete mode 100644 azure/services/virtualmachineimages/cache_test.go delete mode 100644 azure/services/virtualmachineimages/client.go delete mode 100644 azure/services/virtualmachineimages/mock_virtualmachineimages/client_mock.go delete mode 100644 azure/services/virtualmachineimages/mock_virtualmachineimages/doc.go diff --git a/azure/scope/machine_test.go b/azure/scope/machine_test.go index 231d4d4f25c..8755fcc90ab 100644 --- a/azure/scope/machine_test.go +++ b/azure/scope/machine_test.go @@ -43,7 +43,6 @@ import ( "sigs.k8s.io/cluster-api-provider-azure/azure/services/resourceskus" "sigs.k8s.io/cluster-api-provider-azure/azure/services/roleassignments" "sigs.k8s.io/cluster-api-provider-azure/azure/services/virtualmachineimages" - "sigs.k8s.io/cluster-api-provider-azure/azure/services/virtualmachineimages/mock_virtualmachineimages" "sigs.k8s.io/cluster-api-provider-azure/azure/services/vmextensions" ) @@ -1538,7 +1537,7 @@ func TestMachineScope_GetVMImage(t *testing.T) { clusterMock.EXPECT().SubscriptionID().AnyTimes() clusterMock.EXPECT().CloudEnvironment().AnyTimes() clusterMock.EXPECT().Token().Return(&azidentity.DefaultAzureCredential{}).AnyTimes() - svc := virtualmachineimages.Service{Client: mock_virtualmachineimages.NewMockClient(mockCtrl)} + svc := virtualmachineimages.Service{} tests := []struct { name string diff --git a/azure/services/virtualmachineimages/cache.go b/azure/services/virtualmachineimages/cache.go deleted file mode 100644 index 4785bcffbed..00000000000 --- a/azure/services/virtualmachineimages/cache.go +++ /dev/null @@ -1,132 +0,0 @@ -/* -Copyright 2022 The Kubernetes Authors. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -*/ - -package virtualmachineimages - -import ( - "context" - "sync" - "time" - - "github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/compute/armcompute/v5" - "github.com/pkg/errors" - - "sigs.k8s.io/cluster-api-provider-azure/azure" - "sigs.k8s.io/cluster-api-provider-azure/util/cache/ttllru" - "sigs.k8s.io/cluster-api-provider-azure/util/tele" -) - -// Key contains the fields necessary to locate a VM image list resource. -type Key struct { - location string - publicGalleryName string - galleryImageName string -} - -// Cache stores VM image list resources. -type Cache struct { - client Client - data map[Key][]*armcompute.CommunityGalleryImageVersion -} - -// Cacher allows getting items from and adding them to a cache. -type Cacher interface { - Get(key interface{}) (value interface{}, ok bool) - Add(key interface{}, value interface{}) bool -} - -var ( - _ Client = &AzureClient{} - doOnce sync.Once - clientCache Cacher -) - -// newCache instantiates a cache. -func newCache(auth azure.Authorizer) (*Cache, error) { - client, err := NewClient(auth) - if err != nil { - return nil, err - } - return &Cache{ - client: client, - }, nil -} - -// GetCache either creates a new VM images cache or returns the existing one. -func GetCache(auth azure.Authorizer) (*Cache, error) { - var err error - doOnce.Do(func() { - clientCache, err = ttllru.New(128, 1*time.Hour) - }) - if err != nil { - return nil, errors.Wrap(err, "failed creating LRU cache for VM images") - } - - key := auth.HashKey() - c, ok := clientCache.Get(key) - if ok { - return c.(*Cache), nil - } - - c, err = newCache(auth) - if err != nil { - return nil, err - } - _ = clientCache.Add(key, c) - return c.(*Cache), nil -} - -// refresh fetches a VM image list resource from Azure and stores it in the cache. -func (c *Cache) refresh(ctx context.Context, key Key) error { - ctx, _, done := tele.StartSpanWithLogger(ctx, "virtualmachineimages.Cache.refresh") - defer done() - - data, err := c.client.List(ctx, key.location, key.publicGalleryName, key.galleryImageName) - if err != nil { - return errors.Wrap(err, "failed to refresh VM images cache") - } - - c.data[key] = data - - return nil -} - -// Get returns a VM image list resource in a location given a publisher, offer, and sku. -func (c *Cache) Get(ctx context.Context, location, publicGalleryName, galleryImageName string) ([]*armcompute.CommunityGalleryImageVersion, error) { - ctx, log, done := tele.StartSpanWithLogger(ctx, "virtualmachineimages.Cache.Get") - defer done() - - if c.data == nil { - c.data = make(map[Key][]*armcompute.CommunityGalleryImageVersion) - } - - key := Key{ - location: location, - publicGalleryName: publicGalleryName, - galleryImageName: galleryImageName, - } - - if _, ok := c.data[key]; !ok { - log.V(4).Info("VM images cache miss", "location", key.location, "publicGalleryName", key.publicGalleryName, "galleryImageName", key.galleryImageName) - if err := c.refresh(ctx, key); err != nil { - return []*armcompute.CommunityGalleryImageVersion{}, err - } - } else { - log.V(4).Info("VM images cache hit", "location", key.location, "publicGalleryName", key.publicGalleryName, "galleryImageName", key.galleryImageName) - } - - return c.data[key], nil -} diff --git a/azure/services/virtualmachineimages/cache_test.go b/azure/services/virtualmachineimages/cache_test.go deleted file mode 100644 index 020489b04dc..00000000000 --- a/azure/services/virtualmachineimages/cache_test.go +++ /dev/null @@ -1,76 +0,0 @@ -/* -Copyright 2022 The Kubernetes Authors. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -*/ - -package virtualmachineimages - -import ( - "context" - "testing" - - "github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/compute/armcompute/v5" - . "github.com/onsi/gomega" - "github.com/pkg/errors" - "go.uber.org/mock/gomock" - "k8s.io/utils/ptr" - - "sigs.k8s.io/cluster-api-provider-azure/azure/services/virtualmachineimages/mock_virtualmachineimages" -) - -func TestCacheGet(t *testing.T) { - cases := map[string]struct { - location string - publicGalleryName string - galleryImageName string - have []*armcompute.CommunityGalleryImageVersion - expectedError error - }{ - "should find": { - location: "test", publicGalleryName: "foo", galleryImageName: "bar", - have: []*armcompute.CommunityGalleryImageVersion{ - {Name: ptr.To("1.30.5")}, - {Name: ptr.To("1.29.9")}, - }, - expectedError: nil, - }, - "should not find": { - location: "test", publicGalleryName: "foo", galleryImageName: "bar", - have: []*armcompute.CommunityGalleryImageVersion{}, - expectedError: errors.New("failed to refresh VM images cache"), - }, - } - - for name, tc := range cases { - t.Run(name, func(t *testing.T) { - t.Parallel() - - mockCtrl := gomock.NewController(t) - defer mockCtrl.Finish() - mockClient := mock_virtualmachineimages.NewMockClient(mockCtrl) - mockClient.EXPECT().List(gomock.Any(), tc.location, tc.publicGalleryName, tc.galleryImageName).Return(tc.have, tc.expectedError) - c := &Cache{client: mockClient} - - g := NewWithT(t) - val, err := c.Get(context.Background(), tc.location, tc.publicGalleryName, tc.galleryImageName) - if tc.expectedError != nil { - g.Expect(err).To(HaveOccurred()) - g.Expect(err.Error()).To(ContainSubstring(tc.expectedError.Error())) - } else { - g.Expect(err).NotTo(HaveOccurred()) - g.Expect(val).To(Equal(tc.have)) - } - }) - } -} diff --git a/azure/services/virtualmachineimages/client.go b/azure/services/virtualmachineimages/client.go deleted file mode 100644 index 2652c8bed41..00000000000 --- a/azure/services/virtualmachineimages/client.go +++ /dev/null @@ -1,70 +0,0 @@ -/* -Copyright 2022 The Kubernetes Authors. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -*/ - -package virtualmachineimages - -import ( - "context" - - "github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/compute/armcompute/v5" - "github.com/pkg/errors" - - "sigs.k8s.io/cluster-api-provider-azure/azure" - "sigs.k8s.io/cluster-api-provider-azure/util/tele" -) - -// Client is an interface for listing VM images. -type Client interface { - List(ctx context.Context, location, publicGalleryName, galleryImageName string) ([]*armcompute.CommunityGalleryImageVersion, error) -} - -// AzureClient contains the Azure go-sdk Client. -type AzureClient struct { - images *armcompute.CommunityGalleryImageVersionsClient -} - -var _ Client = (*AzureClient)(nil) - -// NewClient creates an AzureClient from an Authorizer. -func NewClient(auth azure.Authorizer) (*AzureClient, error) { - opts, err := azure.ARMClientOptions(auth.CloudEnvironment()) - if err != nil { - return nil, errors.Wrap(err, "failed to create communitygalleryimageversions client options") - } - computeClientFactory, err := armcompute.NewClientFactory(auth.SubscriptionID(), auth.Token(), opts) - if err != nil { - return nil, errors.Wrap(err, "failed to create armcompute client factory") - } - return &AzureClient{computeClientFactory.NewCommunityGalleryImageVersionsClient()}, nil -} - -// List returns a community gallery image versions list response. -func (ac *AzureClient) List(ctx context.Context, location, publicGalleryName, galleryImageName string) ([]*armcompute.CommunityGalleryImageVersion, error) { - ctx, _, done := tele.StartSpanWithLogger(ctx, "virtualmachineimages.AzureClient.List") - defer done() - - responses := make([]*armcompute.CommunityGalleryImageVersion, 0) - pager := ac.images.NewListPager(location, publicGalleryName, galleryImageName, nil) - for pager.More() { - resp, err := pager.NextPage(ctx) - if err != nil { - return nil, errors.Wrap(err, "failed to list image versions") - } - responses = append(responses, resp.CommunityGalleryImageVersionList.Value...) - } - - return responses, nil -} diff --git a/azure/services/virtualmachineimages/images.go b/azure/services/virtualmachineimages/images.go index 53dc343fe0b..972ef7e0b63 100644 --- a/azure/services/virtualmachineimages/images.go +++ b/azure/services/virtualmachineimages/images.go @@ -27,22 +27,25 @@ import ( "sigs.k8s.io/cluster-api-provider-azure/util/tele" ) +/* Reference images live in an Azure community gallery with this structure: +. Gallery: "capzed-489de9a5-a0a0-4e79-a806-ad5479ec43a5" +├── Image Definition: "capi-ubun2-2404" +│ ├── Version: "1.30.4" +│ ├── Version: "1.30.5" +│ └── Version: "1.31.1" +└── Image Definition: "capi-win-2022-containerd" + ├── Version: "1.30.4" + ├── Version: "1.30.5" + └── Version: "1.31.1" +*/ + // Service provides operations on Azure VM Images. type Service struct { - Client - azure.Authorizer } // New creates a VM Images service. -func New(auth azure.Authorizer) (*Service, error) { - client, err := NewClient(auth) - if err != nil { - return nil, err - } - return &Service{ - Client: client, - Authorizer: auth, - }, nil +func New(_ azure.Authorizer) (*Service, error) { + return &Service{}, nil } // GetDefaultLinuxImage returns the default image spec for Ubuntu. diff --git a/azure/services/virtualmachineimages/mock_virtualmachineimages/client_mock.go b/azure/services/virtualmachineimages/mock_virtualmachineimages/client_mock.go deleted file mode 100644 index dd8844892eb..00000000000 --- a/azure/services/virtualmachineimages/mock_virtualmachineimages/client_mock.go +++ /dev/null @@ -1,72 +0,0 @@ -/* -Copyright The Kubernetes Authors. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -*/ - -// Code generated by MockGen. DO NOT EDIT. -// Source: ../client.go -// -// Generated by this command: -// -// mockgen -package mock_virtualmachineimages -destination client_mock.go -source ../client.go -// - -// Package mock_virtualmachineimages is a generated GoMock package. -package mock_virtualmachineimages - -import ( - context "context" - reflect "reflect" - - armcompute "github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/compute/armcompute/v5" - gomock "go.uber.org/mock/gomock" -) - -// MockClient is a mock of Client interface. -type MockClient struct { - ctrl *gomock.Controller - recorder *MockClientMockRecorder -} - -// MockClientMockRecorder is the mock recorder for MockClient. -type MockClientMockRecorder struct { - mock *MockClient -} - -// NewMockClient creates a new mock instance. -func NewMockClient(ctrl *gomock.Controller) *MockClient { - mock := &MockClient{ctrl: ctrl} - mock.recorder = &MockClientMockRecorder{mock} - return mock -} - -// EXPECT returns an object that allows the caller to indicate expected use. -func (m *MockClient) EXPECT() *MockClientMockRecorder { - return m.recorder -} - -// List mocks base method. -func (m *MockClient) List(ctx context.Context, location, publicGalleryName, galleryImageName string) ([]*armcompute.CommunityGalleryImageVersion, error) { - m.ctrl.T.Helper() - ret := m.ctrl.Call(m, "List", ctx, location, publicGalleryName, galleryImageName) - ret0, _ := ret[0].([]*armcompute.CommunityGalleryImageVersion) - ret1, _ := ret[1].(error) - return ret0, ret1 -} - -// List indicates an expected call of List. -func (mr *MockClientMockRecorder) List(ctx, location, publicGalleryName, galleryImageName any) *gomock.Call { - mr.mock.ctrl.T.Helper() - return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "List", reflect.TypeOf((*MockClient)(nil).List), ctx, location, publicGalleryName, galleryImageName) -} diff --git a/azure/services/virtualmachineimages/mock_virtualmachineimages/doc.go b/azure/services/virtualmachineimages/mock_virtualmachineimages/doc.go deleted file mode 100644 index a6cc2935992..00000000000 --- a/azure/services/virtualmachineimages/mock_virtualmachineimages/doc.go +++ /dev/null @@ -1,21 +0,0 @@ -/* -Copyright 2022 The Kubernetes Authors. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -*/ - -// Run go generate to regenerate this mock. -// -//go:generate ../../../../hack/tools/bin/mockgen -package mock_virtualmachineimages -destination client_mock.go -source ../client.go -//go:generate /usr/bin/env bash -c "cat ../../../../hack/boilerplate/boilerplate.generatego.txt client_mock.go > _client_mock.go && mv _client_mock.go client_mock.go" -package mock_virtualmachineimages