Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
flux2
Manage
Activity
Members
Code
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Deploy
Releases
Model registry
Analyze
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
GitLab community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
GitHub Mirror
fluxcd
flux2
Commits
4f43b504
Commit
4f43b504
authored
5 years ago
by
stefanprodan
Browse files
Options
Downloads
Patches
Plain Diff
Assign GitHub teams during bootstrap
parent
62e4b033
No related branches found
No related tags found
No related merge requests found
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
cmd/tk/bootstrap_github.go
+55
-1
55 additions, 1 deletion
cmd/tk/bootstrap_github.go
cmd/tk/get_kustomization.go
+5
-1
5 additions, 1 deletion
cmd/tk/get_kustomization.go
with
60 additions
and
2 deletions
cmd/tk/bootstrap_github.go
+
55
−
1
View file @
4f43b504
...
...
@@ -43,6 +43,9 @@ the bootstrap command will perform an upgrade if needed.`,
# Run bootstrap for a private repo owned by a GitHub organization
bootstrap github --owner=<organization> --repository=<repo name>
# Run bootstrap for a private repo and assign organization teams to it
bootstrap github --owner=<organization> --repository=<repo name> --team=<team1 slug> --team=<team2 slug>
# Run bootstrap for a repository path
bootstrap github --owner=<organization> --repository=<repo name> --path=dev-cluster
...
...
@@ -63,6 +66,7 @@ var (
ghPrivate
bool
ghHostname
string
ghPath
string
ghTeams
[]
string
)
const
(
...
...
@@ -72,11 +76,13 @@ const (
ghSourceManifest
=
"toolkit-source.yaml"
ghKustomizationManifest
=
"toolkit-kustomization.yaml"
ghDefaultHostname
=
"github.com"
ghDefaultPermission
=
"maintain"
)
func
init
()
{
bootstrapGitHubCmd
.
Flags
()
.
StringVar
(
&
ghOwner
,
"owner"
,
""
,
"GitHub user or organization name"
)
bootstrapGitHubCmd
.
Flags
()
.
StringVar
(
&
ghRepository
,
"repository"
,
""
,
"GitHub repository name"
)
bootstrapGitHubCmd
.
Flags
()
.
StringArrayVar
(
&
ghTeams
,
"team"
,
[]
string
{},
"GitHub team to be given maintainer access"
)
bootstrapGitHubCmd
.
Flags
()
.
BoolVar
(
&
ghPersonal
,
"personal"
,
false
,
"is personal repository"
)
bootstrapGitHubCmd
.
Flags
()
.
BoolVar
(
&
ghPrivate
,
"private"
,
true
,
"is private repository"
)
bootstrapGitHubCmd
.
Flags
()
.
DurationVar
(
&
ghInterval
,
"interval"
,
time
.
Minute
,
"sync interval"
)
...
...
@@ -118,6 +124,19 @@ func bootstrapGitHubCmdRun(cmd *cobra.Command, args []string) error {
return
err
}
withErrors
:=
false
// add teams to org repository
if
!
ghPersonal
{
for
_
,
team
:=
range
ghTeams
{
if
err
:=
addGitHubTeam
(
ctx
,
ghHostname
,
ghOwner
,
ghRepository
,
ghToken
,
team
,
ghDefaultPermission
);
err
!=
nil
{
logFailure
(
err
.
Error
())
withErrors
=
true
}
else
{
logSuccess
(
"%s team access granted"
,
team
)
}
}
}
// clone repository and checkout the master branch
repo
,
err
:=
checkoutGitHubRepository
(
ctx
,
ghURL
,
ghBranch
,
ghToken
,
tmpDir
)
if
err
!=
nil
{
...
...
@@ -221,6 +240,10 @@ func bootstrapGitHubCmdRun(cmd *cobra.Command, args []string) error {
}
}
if
withErrors
{
return
fmt
.
Errorf
(
"bootstrap completed with errors"
)
}
logSuccess
(
"bootstrap finished"
)
return
nil
}
...
...
@@ -275,6 +298,37 @@ func createGitHubRepository(ctx context.Context, hostname, owner, name, token st
return
nil
}
func
addGitHubTeam
(
ctx
context
.
Context
,
hostname
,
owner
,
repository
,
token
string
,
teamSlug
,
permission
string
)
error
{
gh
,
err
:=
makeGitHubClient
(
hostname
,
token
)
if
err
!=
nil
{
return
err
}
// check team exists
_
,
_
,
err
=
gh
.
Teams
.
GetTeamBySlug
(
ctx
,
owner
,
teamSlug
)
if
err
!=
nil
{
return
fmt
.
Errorf
(
"github get team %s error: %w"
,
teamSlug
,
err
)
}
// check if team is assigned to the repo
_
,
resp
,
err
:=
gh
.
Teams
.
IsTeamRepoBySlug
(
ctx
,
owner
,
teamSlug
,
owner
,
repository
)
if
resp
==
nil
&&
err
!=
nil
{
return
fmt
.
Errorf
(
"github is team %s error: %w"
,
teamSlug
,
err
)
}
// add team to the repo
if
resp
.
StatusCode
==
404
{
_
,
err
=
gh
.
Teams
.
AddTeamRepoBySlug
(
ctx
,
owner
,
teamSlug
,
owner
,
repository
,
&
github
.
TeamAddTeamRepoOptions
{
Permission
:
permission
,
})
if
err
!=
nil
{
return
fmt
.
Errorf
(
"github add team %s error: %w"
,
teamSlug
,
err
)
}
}
return
nil
}
func
checkoutGitHubRepository
(
ctx
context
.
Context
,
url
,
branch
,
token
,
path
string
)
(
*
git
.
Repository
,
error
)
{
auth
:=
&
http
.
BasicAuth
{
Username
:
"git"
,
...
...
@@ -423,7 +477,7 @@ func generateGitHubKustomization(url, name, namespace, targetPath, tmpDir string
Interval
:
metav1
.
Duration
{
Duration
:
10
*
time
.
Minute
,
},
Path
:
"./"
+
strings
.
TrimPrefix
(
"./"
,
targetPath
),
Path
:
fmt
.
Sprintf
(
"./%s"
,
strings
.
TrimPrefix
(
targetPath
,
"./"
)
),
Prune
:
true
,
SourceRef
:
corev1
.
TypedLocalObjectReference
{
APIGroup
:
&
emptyAPIGroup
,
...
...
This diff is collapsed.
Click to expand it.
cmd/tk/get_kustomization.go
+
5
−
1
View file @
4f43b504
...
...
@@ -51,7 +51,11 @@ func getKsCmdRun(cmd *cobra.Command, args []string) error {
for
_
,
condition
:=
range
kustomization
.
Status
.
Conditions
{
if
condition
.
Type
==
kustomizev1
.
ReadyCondition
{
if
condition
.
Status
!=
corev1
.
ConditionFalse
{
if
kustomization
.
Status
.
LastAppliedRevision
!=
""
{
logSuccess
(
"%s last applied revision %s"
,
kustomization
.
GetName
(),
kustomization
.
Status
.
LastAppliedRevision
)
}
else
{
logSuccess
(
"%s reconciling"
,
kustomization
.
GetName
())
}
}
else
{
logFailure
(
"%s %s"
,
kustomization
.
GetName
(),
condition
.
Message
)
}
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment