Skip to content

Commit e212ee1

Browse files
committed
fixup! feat: Unattended Flux installation
1 parent da56041 commit e212ee1

File tree

3 files changed

+163
-134
lines changed

3 files changed

+163
-134
lines changed

pkg/gitops/deploykey/deploykey.go

Lines changed: 17 additions & 134 deletions
Original file line numberDiff line numberDiff line change
@@ -2,154 +2,37 @@ package deploykey
22

33
import (
44
"context"
5-
"fmt"
65
"os"
7-
"regexp"
86

9-
"github.com/google/go-github/v31/github"
107
"github.com/kris-nova/logger"
11-
api "github.com/weaveworks/eksctl/pkg/apis/eksctl.io/v1alpha5"
8+
"github.com/weaveworks/eksctl/pkg/apis/eksctl.io/v1alpha5"
129
"github.com/weaveworks/eksctl/pkg/gitops/flux"
13-
"golang.org/x/oauth2"
1410
)
1511

16-
type Manager struct {
17-
cluster *api.ClusterMeta
18-
repoURL string
19-
readOnly bool
12+
type GitProvider interface {
13+
Put(ctx context.Context, fluxSSHKey flux.PublicKey) error
14+
Delete(ctx context.Context) error
2015
}
2116

22-
func ForCluster(cluster *api.ClusterConfig) *Manager {
23-
km := &Manager{
24-
cluster: cluster.Metadata,
25-
}
26-
27-
if git := cluster.Git; git != nil {
28-
if repo := git.Repo; repo != nil {
29-
km.repoURL = repo.URL
17+
func ForCluster(cluster *v1alpha5.ClusterConfig) GitProvider {
18+
if githubToken := os.Getenv(EnvVarGitHubToken); githubToken != "" {
19+
km := &GitHubProvider{
20+
cluster: cluster.Metadata,
21+
githubToken: githubToken,
3022
}
3123

32-
km.readOnly = git.ReadOnly
33-
}
34-
35-
return km
36-
}
37-
38-
func (km *Manager) Put(ctx context.Context, fluxSSHKey flux.PublicKey) error {
39-
owner, repo, ok := km.getOwnerRepoFromRepoURL()
40-
if !ok {
41-
logger.Info("Skipped creating GitHub deploy key from Flux SSH public key for URL %s: Only `[email protected]:OWNER/REPO.git` is accepted for automatic deploy key creation", km.repoURL)
42-
43-
return nil
44-
}
45-
46-
gh, ok := km.getGitHubAPIClient(ctx)
47-
if !ok {
48-
logger.Info("Skipped creating GitHub deploy key from Flux SSH public key due to missing GITHUB_TOKEN")
49-
50-
return nil
51-
}
52-
53-
logger.Info("Creating GitHub deploy key from Flux SSH public key")
54-
55-
title := km.getDeployKeyTitle(km.cluster)
56-
57-
key, _, err := gh.Repositories.CreateKey(ctx, owner, repo, &github.Key{
58-
Key: &fluxSSHKey.Key,
59-
Title: &title,
60-
ReadOnly: &km.readOnly,
61-
})
62-
63-
if err != nil {
64-
return err
65-
}
66-
67-
logger.Info("%s configured with Flux SSH public key\n%s", *key.Title, fluxSSHKey.Key)
68-
69-
return nil
70-
}
71-
72-
func (km *Manager) Delete(ctx context.Context) error {
73-
owner, repo, ok := km.getOwnerRepoFromRepoURL()
74-
if !ok {
75-
logger.Info("Skipped deleting GitHub deploy key for URL %s: Only `[email protected]:OWNER/REPO.git` is accepted for automatic deploy key creation", km.repoURL)
76-
77-
return nil
78-
}
79-
80-
gh, ok := km.getGitHubAPIClient(ctx)
81-
if !ok {
82-
logger.Info("Skipped deleting GitHub deploy key due to missing GITHUB_TOKEN")
83-
84-
return nil
85-
}
86-
87-
logger.Info("Deleting GitHub deploy key")
24+
if git := cluster.Git; git != nil {
25+
if repo := git.Repo; repo != nil {
26+
km.repoURL = repo.URL
27+
}
8828

89-
title := km.getDeployKeyTitle(km.cluster)
90-
91-
keys, _, err := gh.Repositories.ListKeys(ctx, owner, repo, &github.ListOptions{})
92-
if err != nil {
93-
return err
94-
}
95-
96-
var keyID int64
97-
98-
for _, key := range keys {
99-
if key.GetTitle() == title {
100-
keyID = key.GetID()
101-
102-
break
29+
km.readOnly = git.ReadOnly
10330
}
104-
}
105-
106-
if keyID == 0 {
107-
logger.Info("Skipped deleting GitHub deploy key %q: The key does not exist. Probably you've already deleted it?")
10831

109-
return nil
32+
return km
11033
}
11134

112-
if _, err := gh.Repositories.DeleteKey(ctx, owner, repo, keyID); err != nil {
113-
return err
114-
}
115-
116-
logger.Info("Deleted GitHub deploy key %s", title)
117-
118-
return nil
119-
}
120-
121-
func (km *Manager) getGitHubAPIClient(ctx context.Context) (*github.Client, bool) {
122-
githubToken := os.Getenv("GITHUB_TOKEN")
123-
124-
if githubToken == "" {
125-
return nil, false
126-
}
127-
128-
ts := oauth2.StaticTokenSource(
129-
&oauth2.Token{AccessToken: githubToken},
130-
)
131-
tc := oauth2.NewClient(ctx, ts)
132-
gh := github.NewClient(tc)
133-
134-
return gh, true
135-
}
136-
137-
func (km *Manager) getOwnerRepoFromRepoURL() (string, string, bool) {
138-
if km.repoURL == "" {
139-
return "", "", false
140-
}
141-
142-
r := regexp.MustCompile(`[email protected]:([^/]+)/([^.]+).git`)
143-
144-
m := r.FindStringSubmatch(km.repoURL)
145-
146-
if len(m) != 3 {
147-
return "", "", false
148-
}
149-
150-
return m[1], m[2], true
151-
}
35+
logger.Info("GITHUB_TOKEN is not set. Please set it so that eksctl is able to create and delete GitHub deploy key from Flux SSH public key")
15236

153-
func (km *Manager) getDeployKeyTitle(cluster *api.ClusterMeta) string {
154-
return fmt.Sprintf("eksctl-flux-%s-%s", cluster.Region, cluster.Name)
37+
return &NoopProvider{}
15538
}

pkg/gitops/deploykey/github.go

Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
package deploykey
2+
3+
import (
4+
"context"
5+
"fmt"
6+
"regexp"
7+
8+
"github.com/google/go-github/v31/github"
9+
"github.com/kris-nova/logger"
10+
api "github.com/weaveworks/eksctl/pkg/apis/eksctl.io/v1alpha5"
11+
"github.com/weaveworks/eksctl/pkg/gitops/flux"
12+
"golang.org/x/oauth2"
13+
)
14+
15+
const (
16+
EnvVarGitHubToken = "GITHUB_TOKEN"
17+
)
18+
19+
type GitHubProvider struct {
20+
cluster *api.ClusterMeta
21+
repoURL string
22+
readOnly bool
23+
githubToken string
24+
}
25+
26+
func (p *GitHubProvider) Put(ctx context.Context, fluxSSHKey flux.PublicKey) error {
27+
owner, repo, ok := p.getOwnerRepoFromRepoURL()
28+
if !ok {
29+
logger.Info("skipped creating GitHub deploy key from Flux SSH public key for URL %s: Only `[email protected]:OWNER/REPO.git` is accepted for automatic deploy key creation", p.repoURL)
30+
31+
return nil
32+
}
33+
34+
gh := p.getGitHubAPIClient(ctx)
35+
36+
logger.Info("creating GitHub deploy key from Flux SSH public key")
37+
38+
title := p.getDeployKeyTitle()
39+
40+
key, _, err := gh.Repositories.CreateKey(ctx, owner, repo, &github.Key{
41+
Key: &fluxSSHKey.Key,
42+
Title: &title,
43+
ReadOnly: &p.readOnly,
44+
})
45+
46+
if err != nil {
47+
return err
48+
}
49+
50+
logger.Info("%s configured with Flux SSH public key\n%s", *key.Title, fluxSSHKey.Key)
51+
52+
return nil
53+
}
54+
55+
func (p *GitHubProvider) Delete(ctx context.Context) error {
56+
owner, repo, ok := p.getOwnerRepoFromRepoURL()
57+
if !ok {
58+
logger.Info("skipped deleting GitHub deploy key for URL %s: Only `[email protected]:OWNER/REPO.git` is accepted for automatic deploy key creation", p.repoURL)
59+
60+
return nil
61+
}
62+
63+
gh := p.getGitHubAPIClient(ctx)
64+
65+
logger.Info("deleting GitHub deploy key")
66+
67+
title := p.getDeployKeyTitle()
68+
69+
keys, _, err := gh.Repositories.ListKeys(ctx, owner, repo, &github.ListOptions{})
70+
if err != nil {
71+
return err
72+
}
73+
74+
var keyID int64
75+
76+
for _, key := range keys {
77+
if key.GetTitle() == title {
78+
keyID = key.GetID()
79+
80+
break
81+
}
82+
}
83+
84+
if keyID == 0 {
85+
logger.Info("skipped deleting GitHub deploy key %q: The key does not exist. Probably you've already deleted it?")
86+
87+
return nil
88+
}
89+
90+
if _, err := gh.Repositories.DeleteKey(ctx, owner, repo, keyID); err != nil {
91+
return err
92+
}
93+
94+
logger.Info("deleted GitHub deploy key %s", title)
95+
96+
return nil
97+
}
98+
99+
func (p *GitHubProvider) getGitHubAPIClient(ctx context.Context) *github.Client {
100+
ts := oauth2.StaticTokenSource(
101+
&oauth2.Token{AccessToken: p.githubToken},
102+
)
103+
tc := oauth2.NewClient(ctx, ts)
104+
gh := github.NewClient(tc)
105+
106+
return gh
107+
}
108+
109+
func (p *GitHubProvider) getOwnerRepoFromRepoURL() (string, string, bool) {
110+
if p.repoURL == "" {
111+
return "", "", false
112+
}
113+
114+
r := regexp.MustCompile(`[email protected]:([^/]+)/([^.]+).git`)
115+
116+
m := r.FindStringSubmatch(p.repoURL)
117+
118+
if len(m) != 3 {
119+
return "", "", false
120+
}
121+
122+
return m[1], m[2], true
123+
}
124+
125+
func (p *GitHubProvider) getDeployKeyTitle() string {
126+
return fmt.Sprintf("eksctl-flux-%s-%s", p.cluster.Region, p.cluster.Name)
127+
}

pkg/gitops/deploykey/noop.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package deploykey
2+
3+
import (
4+
"context"
5+
6+
"github.com/weaveworks/eksctl/pkg/gitops/flux"
7+
)
8+
9+
type NoopProvider struct{}
10+
11+
func (n NoopProvider) Put(ctx context.Context, fluxSSHKey flux.PublicKey) error {
12+
return nil
13+
}
14+
15+
func (n NoopProvider) Delete(ctx context.Context) error {
16+
return nil
17+
}
18+
19+
var _ GitProvider = &NoopProvider{}

0 commit comments

Comments
 (0)