Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🌱 fix duplicate machine tags. #91

Merged
merged 1 commit into from
Jun 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion controllers/hivelocitymachine_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -224,16 +224,22 @@ var _ = Describe("HivelocityMachineReconciler", func() {
testEnv.GetLogger().Info("Machine has wrong providerID")
return false
}
if hvMachine.Spec.Status.ProvisioningState != infrav1.StateDeviceProvisioned {
testEnv.GetLogger().Info("hvMachine.Spec.Status.ProvisioningState != infrav1.StateDeviceProvisioned.",
"ProvisioningState", hvMachine.Spec.Status.ProvisioningState)
return false
}
return true
}, timeout, time.Second).Should(BeTrue())
hvClient := testEnv.HVClientFactory.NewClient("dummy-key")
device, err := hvClient.GetDevice(ctx, mock.FreeDeviceID)
Expect(err).ShouldNot(HaveOccurred())
Expect(device.Tags).Should(BeEquivalentTo([]string{
"caphv-device-type=hvCustom",
"caphv-cluster-name=hv-test1",
"caphv-cluster-hv-test1=owned",
"caphv-cluster-name=hv-test1",
fmt.Sprintf("caphv-machine-name=%s", hvMachine.Name),
"caphv-machine-type=worker",
}))
})
})
Expand Down
2 changes: 1 addition & 1 deletion pkg/scope/machine.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ func (m *MachineScope) DeviceTagMachineType() hvtag.DeviceTag {
value = "worker"
}
return hvtag.DeviceTag{
Key: hvtag.DeviceTagKeyCluster,
Key: hvtag.DeviceTagKeyMachineType,
Value: value,
}
}
Expand Down
2 changes: 2 additions & 0 deletions pkg/services/hivelocity/client/mock/mock_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,8 @@ func (c *mockedHVClient) ProvisionDevice(ctx context.Context, deviceID int32, op
if !ok {
return hv.BareMetalDevice{}, fmt.Errorf("[ProvisionDevice] deviceID %d unknown", deviceID)
}
device.Tags = opts.Tags
c.store.idMap[deviceID] = device
return device, nil
}

Expand Down
14 changes: 14 additions & 0 deletions pkg/services/hivelocity/client/mock/mock_client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"testing"

hvclient "github.com/hivelocity/cluster-api-provider-hivelocity/pkg/services/hivelocity/client"
hv "github.com/hivelocity/hivelocity-client-go/client"
"github.com/stretchr/testify/require"
)

Expand Down Expand Up @@ -71,3 +72,16 @@ func Test_NewMockedHVClientFactory(t *testing.T) {
require.NoError(t, err)
require.ElementsMatch(t, device.Tags, []string{"caphv-device-type=hvCustom"})
}

func Test_ProvisionDevice(t *testing.T) {
factory := NewMockedHVClientFactory()
client := factory.NewClient("dummy-key")
ctx := context.Background()
_, err := client.ProvisionDevice(ctx, FreeDeviceID, hv.BareMetalDeviceUpdate{
Tags: []string{"dummyTag"},
})
require.NoError(t, err)
device, err := client.GetDevice(ctx, FreeDeviceID)
require.NoError(t, err)
require.ElementsMatch(t, []string{"dummyTag"}, device.Tags)
}
11 changes: 2 additions & 9 deletions pkg/services/hivelocity/device/device.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,9 +116,10 @@ func (s *Service) actionAssociateDevice(ctx context.Context) actionResult {

// associate this device with the machine object by setting tags
device.Tags = append(device.Tags,
s.scope.HivelocityCluster.DeviceTag().ToString(),
s.scope.HivelocityCluster.DeviceTagOwned().ToString(),
s.scope.HivelocityCluster.DeviceTag().ToString(),
s.scope.HivelocityMachine.DeviceTag().ToString(),
s.scope.DeviceTagMachineType().ToString(),
)

if err := s.scope.HVClient.SetDeviceTags(ctx, device.DeviceId, device.Tags); err != nil {
Expand Down Expand Up @@ -299,14 +300,6 @@ func (s *Service) actionProvisionDevice(ctx context.Context) actionResult {
return actionError{err: fmt.Errorf("failed to get device image: %w", err)}
}

tags := []string{
s.scope.HivelocityCluster.DeviceTag().ToString(),
s.scope.HivelocityMachine.DeviceTag().ToString(),
s.scope.DeviceTagMachineType().ToString(),
}

device.Tags = append(device.Tags, tags...)

opts := hv.BareMetalDeviceUpdate{
Hostname: fmt.Sprintf("%s.example.com", s.scope.Name()), // TODO: HV API requires a FQDN.
Tags: device.Tags,
Expand Down