Skip to content

Commit 0ff27a3

Browse files
Domas Monkus0x2b3bfa0
andauthored
Handle instance permission set in k8s runner resource. (#682)
Co-authored-by: Helio Machado <0x2b3bfa0+git@googlemail.com>
1 parent 16ab9af commit 0ff27a3

File tree

2 files changed

+24
-2
lines changed

2 files changed

+24
-2
lines changed

iterative/kubernetes/provider.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"fmt"
88
"io"
99
"log"
10+
"net/http"
1011
"os"
1112
"strconv"
1213
"time"
@@ -83,6 +84,12 @@ func ResourceMachineCreate(ctx context.Context, d *terraform_schema.ResourceData
8384
}
8485
}
8586

87+
// Lookup service account if set.
88+
svcAccount, svcTokenAutomount, err := getServiceAccount(ctx, conn, namespace, d.Get("instance_permission_set").(string))
89+
if err != nil {
90+
return err
91+
}
92+
8693
// Leave the job running for 30 seconds after the termination signal, but remove it immediately after terminating.
8794
jobTTLSecondsAfterFinished := int32(0)
8895
jobTerminationGracePeriod := int64(30)
@@ -126,6 +133,8 @@ func ResourceMachineCreate(ctx context.Context, d *terraform_schema.ResourceData
126133
},
127134
},
128135
},
136+
ServiceAccountName: svcAccount,
137+
AutomountServiceAccountToken: svcTokenAutomount,
129138
},
130139
},
131140
},
@@ -434,3 +443,17 @@ func ResourceMachineLogs(ctx context.Context, d *terraform_schema.ResourceData,
434443

435444
return buf.String(), nil
436445
}
446+
447+
func getServiceAccount(ctx context.Context, client kubernetes.Interface, namespace string, accountName string) (account string, automountToken *bool, err error) {
448+
if accountName == "" {
449+
return "", nil, nil
450+
}
451+
acct, err := client.CoreV1().ServiceAccounts(namespace).Get(ctx, accountName, kubernetes_meta.GetOptions{})
452+
if err != nil {
453+
if statusErr, ok := err.(*kubernetes_errors.StatusError); ok && statusErr.ErrStatus.Code == http.StatusNotFound {
454+
return "", nil, fmt.Errorf("service account %q does not exist in namespace %q", accountName, namespace)
455+
}
456+
return "", nil, fmt.Errorf("failed to lookup service account %q in namespace %q: %w", accountName, namespace, err)
457+
}
458+
return accountName, acct.AutomountServiceAccountToken, nil
459+
}

task/k8s/resources/data_source_permission_set.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,7 @@ func (ps *PermissionSet) Read(ctx context.Context) error {
4141
return fmt.Errorf("service account %q does not exist in namespace %q: %w",
4242
ps.Identifier, ps.client.Namespace, common.NotFoundError)
4343
}
44-
return fmt.Errorf("failed to lookup service account %q in namespace %q: %w",
45-
ps.Identifier, ps.client.Namespace, common.NotFoundError)
44+
return fmt.Errorf("failed to lookup service account %q in namespace %q: %w", ps.Identifier, ps.client.Namespace, err)
4645

4746
}
4847
ps.Resource.ServiceAccountName = ps.Identifier

0 commit comments

Comments
 (0)