-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Unattended Flux installation for GitHub repos (automatic add of deploy key) #2274
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
9ac1704
feat: Unattended Flux installation
mumoshu 8e6afca
fixup! feat: Unattended Flux installation
mumoshu 2dba2c8
fixup! fixup! feat: Unattended Flux installation
mumoshu d14cb1a
fixup! fixup! fixup! feat: Unattended Flux installation
mumoshu 29d7368
Merge branch 'master' into unattended-flux-setup
mumoshu 5fc72ea
Merge branch 'master' into unattended-flux-setup
martina-if File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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,74 @@ | ||
package deploykey | ||
|
||
import ( | ||
"context" | ||
"os" | ||
|
||
"github.com/kris-nova/logger" | ||
"github.com/weaveworks/eksctl/pkg/apis/eksctl.io/v1alpha5" | ||
) | ||
|
||
type GitProvider interface { | ||
Put(ctx context.Context, fluxSSHKey PublicKey) error | ||
Delete(ctx context.Context) error | ||
} | ||
|
||
func ForCluster(cluster *v1alpha5.ClusterConfig) GitProvider { | ||
var ( | ||
repoURL string | ||
readOnly bool | ||
) | ||
|
||
if git := cluster.Git; git != nil { | ||
if repo := git.Repo; repo != nil { | ||
repoURL = repo.URL | ||
} | ||
|
||
readOnly = git.Operator.ReadOnly | ||
} | ||
|
||
if repoURL == "" { | ||
return nil | ||
} | ||
|
||
if owner, repo, ok := getGitHubOwnerRepoFromRepoURL(repoURL); !ok { | ||
logger.Info("skipped managing GitHub deploy key for URL %s: Only `[email protected]:OWNER/REPO.git` is accepted for automatic deploy key creation", repoURL) | ||
} else if githubToken := os.Getenv(EnvVarGitHubToken); githubToken == "" { | ||
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") | ||
} else { | ||
return &GitHubProvider{ | ||
cluster: cluster.Metadata, | ||
githubToken: githubToken, | ||
readOnly: readOnly, | ||
owner: owner, | ||
repo: repo, | ||
} | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func Put(ctx context.Context, cluster *v1alpha5.ClusterConfig, fluxSSHKey PublicKey) (bool, error) { | ||
p := ForCluster(cluster) | ||
|
||
if p == nil { | ||
return false, nil | ||
} | ||
|
||
return true, p.Put(ctx, fluxSSHKey) | ||
} | ||
|
||
func Delete(ctx context.Context, cluster *v1alpha5.ClusterConfig) error { | ||
p := ForCluster(cluster) | ||
|
||
if p == nil { | ||
return nil | ||
} | ||
|
||
return p.Delete(ctx) | ||
} | ||
|
||
// PublicKey represents a public SSH key as it is returned by flux | ||
type PublicKey struct { | ||
Key string | ||
} |
This file contains hidden or 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,119 @@ | ||
package deploykey | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"regexp" | ||
|
||
"github.com/google/go-github/v31/github" | ||
"github.com/kris-nova/logger" | ||
api "github.com/weaveworks/eksctl/pkg/apis/eksctl.io/v1alpha5" | ||
"golang.org/x/oauth2" | ||
) | ||
|
||
const ( | ||
EnvVarGitHubToken = "GITHUB_TOKEN" | ||
) | ||
|
||
type GitHubProvider struct { | ||
cluster *api.ClusterMeta | ||
owner, repo string | ||
readOnly bool | ||
githubToken string | ||
} | ||
|
||
func (p *GitHubProvider) Put(ctx context.Context, fluxSSHKey PublicKey) error { | ||
gh := p.getGitHubAPIClient(ctx) | ||
|
||
logger.Info("creating GitHub deploy key from Flux SSH public key") | ||
|
||
title := p.getDeployKeyTitle() | ||
|
||
key, _, err := gh.Repositories.CreateKey(ctx, p.owner, p.repo, &github.Key{ | ||
Key: &fluxSSHKey.Key, | ||
Title: &title, | ||
ReadOnly: &p.readOnly, | ||
}) | ||
|
||
if err != nil { | ||
return err | ||
} | ||
|
||
logger.Info("%s configured with Flux SSH public key\n%s", *key.Title, fluxSSHKey.Key) | ||
|
||
return nil | ||
} | ||
|
||
func (p *GitHubProvider) Delete(ctx context.Context) error { | ||
gh := p.getGitHubAPIClient(ctx) | ||
|
||
logger.Info("deleting GitHub deploy key") | ||
|
||
title := p.getDeployKeyTitle() | ||
|
||
keys, _, err := gh.Repositories.ListKeys(ctx, p.owner, p.repo, &github.ListOptions{}) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
var keyID int64 | ||
|
||
for _, key := range keys { | ||
if key.GetTitle() == title { | ||
keyID = key.GetID() | ||
|
||
break | ||
} | ||
} | ||
|
||
if keyID == 0 { | ||
logger.Info("skipped deleting GitHub deploy key %q: The key does not exist. Probably you've already deleted it?") | ||
|
||
return nil | ||
} | ||
|
||
if _, err := gh.Repositories.DeleteKey(ctx, p.owner, p.repo, keyID); err != nil { | ||
return err | ||
} | ||
|
||
logger.Info("deleted GitHub deploy key %s", title) | ||
|
||
return nil | ||
} | ||
|
||
func (p *GitHubProvider) getGitHubAPIClient(ctx context.Context) *github.Client { | ||
martina-if marked this conversation as resolved.
Show resolved
Hide resolved
|
||
ts := oauth2.StaticTokenSource( | ||
&oauth2.Token{AccessToken: p.githubToken}, | ||
) | ||
tc := oauth2.NewClient(ctx, ts) | ||
gh := github.NewClient(tc) | ||
|
||
return gh | ||
} | ||
|
||
func getGitHubOwnerRepoFromRepoURL(repoURL string) (string, string, bool) { | ||
if repoURL == "" { | ||
return "", "", false | ||
} | ||
|
||
sshFull := regexp.MustCompile(`ssh://[email protected]/([^/]+)/([^.]+).git`) | ||
sshShort := regexp.MustCompile(`[email protected]:([^/]+)/([^.]+).git`) | ||
|
||
patterns := []*regexp.Regexp{ | ||
sshFull, | ||
sshShort, | ||
} | ||
|
||
for _, p := range patterns { | ||
m := p.FindStringSubmatch(repoURL) | ||
if len(m) == 3 { | ||
return m[1], m[2], true | ||
} | ||
} | ||
|
||
return "", "", false | ||
} | ||
|
||
func (p *GitHubProvider) getDeployKeyTitle() string { | ||
return fmt.Sprintf("eksctl-flux-%s-%s", p.cluster.Region, p.cluster.Name) | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.