Skip to content

Commit

Permalink
Update EC2 instace tags when running in CI mode
Browse files Browse the repository at this point in the history
Signed-off-by: Carlos Eduardo Arango Gutierrez <[email protected]>
  • Loading branch information
ArangoGutierrez committed May 28, 2024
1 parent 654f3d4 commit eb4e8a3
Show file tree
Hide file tree
Showing 3 changed files with 91 additions and 3 deletions.
34 changes: 33 additions & 1 deletion cmd/action/ci/ci.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ import (

"github.com/NVIDIA/holodeck/api/holodeck/v1alpha1"
"github.com/NVIDIA/holodeck/internal/logger"
"github.com/aws/aws-sdk-go-v2/service/ec2/types"
"github.com/aws/aws-sdk-go/aws"
)

const (
Expand Down Expand Up @@ -73,8 +75,38 @@ func generateUID() string {

b := make([]byte, 8)
for i := range b {
b[i] = charset[rand.Intn(len(charset))]
b[i] = charset[rand.Intn(len(charset))] //nolint:gosec // We use randomization for appending unique suffixes and this need not be cryptographically secure.
}

return string(b)
}

// instanceTags returns the tags to be applied to the EC2 instance
// based on the GitHub environment variables https://docs.github.com/en/actions/learn-github-actions/variables
func instanceTags() []types.Tag {
job := os.Getenv("GITHUB_JOB")
repository := os.Getenv("GITHUB_REPOSITORY")
actor := os.Getenv("GITHUB_ACTOR")
sha := os.Getenv("GITHUB_SHA")

tags := []types.Tag{
{
Key: aws.String("GITHUB_JOB"),
Value: aws.String(job),
},
{
Key: aws.String("GITHUB_REPOSITORY"),
Value: aws.String(repository),
},
{
Key: aws.String("GITHUB_ACTOR"),
Value: aws.String(actor),
},
{
Key: aws.String("GITHUB_SHA"),
Value: aws.String(sha),
},
}

return tags
}
16 changes: 14 additions & 2 deletions cmd/action/ci/entrypoint.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,13 +112,25 @@ func entrypoint(log *logger.FunLogger) error {
}

var hostUrl string
var instanceID string
for _, p := range cache.Status.Properties {
if p.Name == aws.PublicDnsName {
switch p.Name {
case aws.PublicDnsName:
hostUrl = p.Value
break
case aws.InstanceID:
instanceID = p.Value
default:
continue
}
}

// Tag the instance with info the GitHub event
tags := instanceTags()
err = client.UpdateEC2InstanceTags(instanceID, tags)
if err != nil {
return err
}

p, err := provisioner.New(log, sshKeyFile, username, hostUrl)
if err != nil {
return err
Expand Down
44 changes: 44 additions & 0 deletions pkg/provider/aws/update.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*
* Copyright (c) 2023, NVIDIA CORPORATION. All rights reserved.
*
* 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 aws

import (
"context"

"github.com/aws/aws-sdk-go-v2/service/ec2"
"github.com/aws/aws-sdk-go-v2/service/ec2/types"
)

// Update updates an AWS instance
func (a *Client) UpdateEC2InstanceTags(InstanceID string, tags []types.Tag) error {
a.log.Wg.Add(1)
go a.log.Loading("Creating EC2 instance")

createTagsIn := &ec2.CreateTagsInput{
Resources: []string{InstanceID},
Tags: tags,
}

_, err := a.ec2.CreateTags(context.Background(), createTagsIn)
if err != nil {
a.fail()
return err
}
a.done()

return nil
}

0 comments on commit eb4e8a3

Please sign in to comment.