/
apiserviceaccount.go
54 lines (46 loc) · 1.51 KB
/
apiserviceaccount.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
package create
import (
"context"
"time"
runtimev1 "github.com/crossplane/crossplane-runtime/apis/common/v1"
iam "github.com/ninech/apis/iam/v1alpha1"
"github.com/ninech/nctl/api"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)
type apiServiceAccountCmd struct {
Name string `arg:"" default:"" help:"Name of the API Service Account. A random name is generated if omitted."`
Wait bool `default:"true" help:"Wait until API Service Account is fully created."`
WaitTimeout time.Duration `default:"10s" help:"Duration to wait for API Service Account getting ready. Only relevant if wait is set."`
}
func (asa *apiServiceAccountCmd) Run(ctx context.Context, client *api.Client) error {
c := newCreator(client, asa.newAPIServiceAccount(client.Project), iam.APIServiceAccountKind)
ctx, cancel := context.WithTimeout(ctx, asa.WaitTimeout)
defer cancel()
if err := c.createResource(ctx); err != nil {
return err
}
if !asa.Wait {
return nil
}
return c.wait(ctx, waitStage{
objectList: &iam.APIServiceAccountList{},
onResult: resourceAvailable,
})
}
func (asa *apiServiceAccountCmd) newAPIServiceAccount(project string) *iam.APIServiceAccount {
name := getName(asa.Name)
return &iam.APIServiceAccount{
ObjectMeta: metav1.ObjectMeta{
Name: name,
Namespace: project,
},
Spec: iam.APIServiceAccountSpec{
ResourceSpec: runtimev1.ResourceSpec{
WriteConnectionSecretToReference: &runtimev1.SecretReference{
Name: name,
Namespace: project,
},
},
},
}
}