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
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
GitHub Mirror
fluxcd
flux2
Commits
4f449a1f
Commit
4f449a1f
authored
5 years ago
by
stefanprodan
Browse files
Options
Downloads
Patches
Plain Diff
Add check command
parent
c8fdf760
No related branches found
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
cmd/tk/check.go
+103
-0
103 additions, 0 deletions
cmd/tk/check.go
with
103 additions
and
0 deletions
cmd/tk/check.go
0 → 100644
+
103
−
0
View file @
4f449a1f
package
main
import
(
"fmt"
"os"
"os/exec"
"path/filepath"
"github.com/spf13/cobra"
"k8s.io/client-go/kubernetes"
_
"k8s.io/client-go/plugin/pkg/client/auth"
"k8s.io/client-go/tools/clientcmd"
)
var
checkCmd
=
&
cobra
.
Command
{
Use
:
"check --pre"
,
Short
:
"Check for potential problems"
,
Long
:
`
The check command will perform a series of checks to validate that
the local environment and Kubernetes cluster are configured correctly.`
,
Example
:
` check --pre`
,
RunE
:
runCheckCmd
,
}
var
(
kubeconfig
string
checkPre
bool
)
func
init
()
{
if
home
:=
homeDir
();
home
!=
""
{
checkCmd
.
Flags
()
.
StringVarP
(
&
kubeconfig
,
"kubeconfig"
,
""
,
filepath
.
Join
(
home
,
".kube"
,
"config"
),
"path to the kubeconfig file"
)
}
else
{
checkCmd
.
Flags
()
.
StringVarP
(
&
kubeconfig
,
"kubeconfig"
,
""
,
""
,
"absolute path to the kubeconfig file"
)
}
checkCmd
.
Flags
()
.
BoolVarP
(
&
checkPre
,
"pre"
,
""
,
false
,
"only run pre-installation checks"
)
rootCmd
.
AddCommand
(
checkCmd
)
}
func
runCheckCmd
(
cmd
*
cobra
.
Command
,
args
[]
string
)
error
{
if
!
checkLocal
()
{
os
.
Exit
(
1
)
}
if
checkPre
{
fmt
.
Println
(
`✔`
,
"all prerequisites checks passed"
)
return
nil
}
if
!
checkRemote
()
{
os
.
Exit
(
1
)
}
else
{
fmt
.
Println
(
`✔`
,
"all checks passed"
)
}
return
nil
}
func
homeDir
()
string
{
if
h
:=
os
.
Getenv
(
"HOME"
);
h
!=
""
{
return
h
}
return
os
.
Getenv
(
"USERPROFILE"
)
// windows
}
func
checkLocal
()
bool
{
ok
:=
true
for
_
,
cmd
:=
range
[]
string
{
"kubectl"
,
"kustomize"
}
{
_
,
err
:=
exec
.
LookPath
(
cmd
)
if
err
!=
nil
{
fmt
.
Println
(
`✗`
,
cmd
,
"not found"
)
ok
=
false
}
else
{
fmt
.
Println
(
`✔`
,
cmd
,
"found"
)
}
}
return
ok
}
func
checkRemote
()
bool
{
config
,
err
:=
clientcmd
.
BuildConfigFromFlags
(
""
,
kubeconfig
)
if
err
!=
nil
{
fmt
.
Println
(
`✗`
,
"kubernetes client initialization failed"
,
err
.
Error
())
return
false
}
client
,
err
:=
kubernetes
.
NewForConfig
(
config
)
if
err
!=
nil
{
fmt
.
Println
(
`✗`
,
"kubernetes client initialization failed"
,
err
.
Error
())
return
false
}
ver
,
err
:=
client
.
Discovery
()
.
ServerVersion
()
if
err
!=
nil
{
fmt
.
Println
(
`✗`
,
"kubernetes API call failed"
,
err
.
Error
())
return
false
}
fmt
.
Println
(
`✔`
,
"kubernetes version"
,
ver
.
String
())
return
true
}
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