-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add support for stackscript based bootstrapping (#211)
* add support for stackscript based bootstrapping
- Loading branch information
1 parent
b78416f
commit e3566e4
Showing
16 changed files
with
400 additions
and
83 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
#!/bin/sh | ||
# <UDF name="instancedata" label="instance-data contents(base64 encoded" /> | ||
# <UDF name="userdata" label="user-data file contents (base64 encoded)" /> | ||
|
||
cat > /etc/cloud/cloud.cfg.d/100_none.cfg <<EOF | ||
datasource_list: [ "None"] | ||
datasource: | ||
None: | ||
metadata: | ||
id: $LINODE_ID | ||
$(echo "${INSTANCEDATA}" | base64 -d | sed "s/^/ /") | ||
userdata_raw: | | ||
$(echo "${USERDATA}" | base64 -d | sed "s/^/ /") | ||
EOF | ||
|
||
cloud-init clean | ||
cloud-init -f /etc/cloud/cloud.cfg.d/100_none.cfg init --local | ||
cloud-init -f /etc/cloud/cloud.cfg.d/100_none.cfg init | ||
cloud-init -f /etc/cloud/cloud.cfg.d/100_none.cfg modules --mode=config | ||
cloud-init -f /etc/cloud/cloud.cfg.d/100_none.cfg modules --mode=final |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package services | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"net/http" | ||
|
||
"github.com/linode/linodego" | ||
|
||
"github.com/linode/cluster-api-provider-linode/cloud/scope" | ||
"github.com/linode/cluster-api-provider-linode/util" | ||
"github.com/linode/cluster-api-provider-linode/version" | ||
|
||
_ "embed" | ||
) | ||
|
||
//go:embed stackscript.sh | ||
var stackscriptTemplate string | ||
|
||
func EnsureStackscript(ctx context.Context, machineScope *scope.MachineScope) (int, error) { | ||
stackscriptName := fmt.Sprintf("CAPL-%s", version.GetVersion()) | ||
listFilter := util.Filter{ | ||
ID: nil, | ||
Label: stackscriptName, | ||
Tags: []string{}, | ||
} | ||
filter, err := listFilter.String() | ||
if err != nil { | ||
return 0, err | ||
} | ||
stackscripts, err := machineScope.LinodeClient.ListStackscripts(ctx, &linodego.ListOptions{Filter: filter}) | ||
if util.IgnoreLinodeAPIError(err, http.StatusNotFound) != nil { | ||
return 0, fmt.Errorf("failed to get stackscript with label %s: %w", stackscriptName, err) | ||
} | ||
if stackscripts != nil { | ||
return stackscripts[0].ID, nil | ||
} | ||
stackscriptCreateOptions := linodego.StackscriptCreateOptions{ | ||
Label: fmt.Sprintf("CAPL-%s", version.GetVersion()), | ||
Description: fmt.Sprintf("Stackscript for creating CAPL clusters with CAPL controller version %s", version.GetVersion()), | ||
Script: stackscriptTemplate, | ||
Images: []string{"any/all"}, | ||
} | ||
stackscript, err := machineScope.LinodeClient.CreateStackscript(ctx, stackscriptCreateOptions) | ||
if err != nil { | ||
return 0, fmt.Errorf("failed to create StackScript: %w", err) | ||
} | ||
|
||
return stackscript.ID, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
package services | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"testing" | ||
|
||
"github.com/linode/linodego" | ||
"github.com/stretchr/testify/assert" | ||
"go.uber.org/mock/gomock" | ||
|
||
"github.com/linode/cluster-api-provider-linode/cloud/scope" | ||
"github.com/linode/cluster-api-provider-linode/mock" | ||
) | ||
|
||
func TestEnsureStackscripts(t *testing.T) { | ||
t.Parallel() | ||
|
||
tests := []struct { | ||
name string | ||
machineScope *scope.MachineScope | ||
want int | ||
expectedError error | ||
expects func(client *mock.MockLinodeMachineClient) | ||
}{ | ||
{ | ||
name: "Success - Successfully get existing StackScript", | ||
machineScope: &scope.MachineScope{}, | ||
want: 1234, | ||
expects: func(mockClient *mock.MockLinodeMachineClient) { | ||
mockClient.EXPECT().ListStackscripts(gomock.Any(), &linodego.ListOptions{Filter: "{\"label\":\"CAPL-dev\"}"}).Return([]linodego.Stackscript{{ | ||
Label: "CAPI Test 1", | ||
ID: 1234, | ||
}}, nil) | ||
}, | ||
}, | ||
{ | ||
name: "Error - failed get existing StackScript", | ||
machineScope: &scope.MachineScope{}, | ||
expects: func(mockClient *mock.MockLinodeMachineClient) { | ||
mockClient.EXPECT().ListStackscripts(gomock.Any(), gomock.Any()).Return(nil, fmt.Errorf("failed to get StackScript")) | ||
}, | ||
expectedError: fmt.Errorf("failed to get StackScript"), | ||
}, | ||
{ | ||
name: "Success - Successfully created StackScript", | ||
machineScope: &scope.MachineScope{}, | ||
want: 56345, | ||
expects: func(mockClient *mock.MockLinodeMachineClient) { | ||
mockClient.EXPECT().ListStackscripts(gomock.Any(), gomock.Any()).Return(nil, nil) | ||
mockClient.EXPECT().CreateStackscript(gomock.Any(), linodego.StackscriptCreateOptions{ | ||
Label: "CAPL-dev", | ||
Description: "Stackscript for creating CAPL clusters with CAPL controller version dev", | ||
Script: `#!/bin/sh | ||
# <UDF name="instancedata" label="instance-data contents(base64 encoded" /> | ||
# <UDF name="userdata" label="user-data file contents (base64 encoded)" /> | ||
cat > /etc/cloud/cloud.cfg.d/100_none.cfg <<EOF | ||
datasource_list: [ "None"] | ||
datasource: | ||
None: | ||
metadata: | ||
id: $LINODE_ID | ||
$(echo "${INSTANCEDATA}" | base64 -d | sed "s/^/ /") | ||
userdata_raw: | | ||
$(echo "${USERDATA}" | base64 -d | sed "s/^/ /") | ||
EOF | ||
cloud-init clean | ||
cloud-init -f /etc/cloud/cloud.cfg.d/100_none.cfg init --local | ||
cloud-init -f /etc/cloud/cloud.cfg.d/100_none.cfg init | ||
cloud-init -f /etc/cloud/cloud.cfg.d/100_none.cfg modules --mode=config | ||
cloud-init -f /etc/cloud/cloud.cfg.d/100_none.cfg modules --mode=final | ||
`, | ||
Images: []string{"any/all"}, | ||
}).Return(&linodego.Stackscript{ | ||
Label: "CAPI Test 1", | ||
ID: 56345, | ||
}, nil) | ||
}, | ||
}, | ||
{ | ||
name: "Error - failed create StackScript", | ||
machineScope: &scope.MachineScope{}, | ||
expects: func(mockClient *mock.MockLinodeMachineClient) { | ||
mockClient.EXPECT().ListStackscripts(gomock.Any(), gomock.Any()).Return(nil, nil) | ||
mockClient.EXPECT().CreateStackscript(gomock.Any(), gomock.Any()).Return(nil, fmt.Errorf("failed to create StackScript")) | ||
}, | ||
expectedError: fmt.Errorf("failed to create StackScript"), | ||
}, | ||
} | ||
for _, tt := range tests { | ||
testcase := tt | ||
t.Run(testcase.name, func(t *testing.T) { | ||
t.Parallel() | ||
|
||
ctrl := gomock.NewController(t) | ||
defer ctrl.Finish() | ||
|
||
mockClient := mock.NewMockLinodeMachineClient(ctrl) | ||
|
||
testcase.machineScope.LinodeClient = mockClient | ||
|
||
testcase.expects(mockClient) | ||
|
||
got, err := EnsureStackscript(context.Background(), testcase.machineScope) | ||
if testcase.expectedError != nil { | ||
assert.ErrorContains(t, err, testcase.expectedError.Error()) | ||
} else { | ||
assert.Equal(t, testcase.want, got) | ||
} | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.