@ -23,14 +23,17 @@ import (
"os/exec"
"time"
"github.com/ blang/semver/v4 "
"github.com/ Masterminds/semver/v3 "
"github.com/spf13/cobra"
v1 "k8s.io/api/apps/v1"
apimachineryversion "k8s.io/apimachinery/pkg/version"
"k8s.io/client-go/kubernetes"
"sigs.k8s.io/controller-runtime/pkg/client"
"github.com/fluxcd/pkg/version"
"github.com/fluxcd/flux2/internal/utils"
"github.com/fluxcd/flux2/pkg/manifestgen/install"
)
var checkCmd = & cobra . Command {
@ -76,6 +79,8 @@ func runCheckCmd(cmd *cobra.Command, args []string) error {
logger . Actionf ( "checking prerequisites" )
checkFailed := false
fluxCheck ( )
if ! kubectlCheck ( ctx , ">=1.18.0" ) {
checkFailed = true
}
@ -103,7 +108,29 @@ func runCheckCmd(cmd *cobra.Command, args []string) error {
return nil
}
func kubectlCheck ( ctx context . Context , version string ) bool {
func fluxCheck ( ) {
curSv , err := version . ParseVersion ( VERSION )
if err != nil {
return
}
// Exclude development builds.
if curSv . Prerelease ( ) != "" {
return
}
latest , err := install . GetLatestVersion ( )
if err != nil {
return
}
latestSv , err := version . ParseVersion ( latest )
if err != nil {
return
}
if latestSv . GreaterThan ( curSv ) {
logger . Failuref ( "flux %s <%s (new version is available, please upgrade)" , curSv , latestSv )
}
}
func kubectlCheck ( ctx context . Context , constraint string ) bool {
_ , err := exec . LookPath ( "kubectl" )
if err != nil {
logger . Failuref ( "kubectl not found" )
@ -119,58 +146,58 @@ func kubectlCheck(ctx context.Context, version string) bool {
kv := & kubectlVersion { }
if err = json . Unmarshal ( [ ] byte ( output ) , kv ) ; err != nil {
logger . Failuref ( "kubectl version output can't be unmarshal ed")
logger . Failuref ( "kubectl version output can't be unmarshal l ed")
return false
}
v , err := semver. ParseTolerant ( kv . ClientVersion . GitVersion )
v , err := version. ParseVersion ( kv . ClientVersion . GitVersion )
if err != nil {
logger . Failuref ( "kubectl version can't be parsed" )
return false
}
rng, _ := semver . ParseRange ( version )
if ! rng ( v ) {
logger . Failuref ( "kubectl version must be %s" , version )
c, _ := semver . NewConstraint ( constraint )
if ! c . Check ( v ) {
logger . Failuref ( "kubectl version must be %s" , constraint )
return false
}
logger . Successf ( "kubectl %s %s" , v . String ( ) , version )
logger . Successf ( "kubectl %s %s" , v . String ( ) , constraint )
return true
}
func kubernetesCheck ( version string ) bool {
func kubernetesCheck ( constraint string ) bool {
cfg , err := utils . KubeConfig ( rootArgs . kubeconfig , rootArgs . kubecontext )
if err != nil {
logger . Failuref ( "Kubernetes client initialization failed: %s" , err . Error ( ) )
return false
}
client , err := kubernetes . NewForConfig ( cfg )
client Set , err := kubernetes . NewForConfig ( cfg )
if err != nil {
logger . Failuref ( "Kubernetes client initialization failed: %s" , err . Error ( ) )
return false
}
ver , err := clien t. Discovery ( ) . ServerVersion ( )
k v, err := clien tSe t. Discovery ( ) . ServerVersion ( )
if err != nil {
logger . Failuref ( "Kubernetes API call failed: %s" , err . Error ( ) )
return false
}
v , err := semver. ParseTolerant ( ver . String ( ) )
v , err := version. ParseVersion ( kv . String ( ) )
if err != nil {
logger . Failuref ( "Kubernetes version can't be determined" )
return false
}
rng, _ := semver . ParseRange ( version )
if ! rng ( v ) {
logger . Failuref ( "Kubernetes version must be %s" , version )
c, _ := semver . NewConstraint ( constraint )
if ! c . Check ( v ) {
logger . Failuref ( "Kubernetes version must be %s" , constraint )
return false
}
logger . Successf ( "Kubernetes %s %s" , v . String ( ) , version )
logger . Successf ( "Kubernetes %s %s" , v . String ( ) , constraint )
return true
}