// # CheckKubeconfig return two strings
// first is the complate guide
// second is a simple msg
# CheckKubeconfig return two strings
first is the complate guide
second is a simple msg
// # CheckKubeconfig return two strings // first is the complate guide // second is a simple msg
func CheckKubeconfig(kubeconfigParams string, contextParam string) CheckInfo {
	utils, err := clientgoutils.NewClientGoUtils(kubeconfigParams, "")
	if err != nil {
		return CheckInfo{
			FAIL, InfoInvalid, err.Error(), true, err.Error(),
		}
	}
	config, err := utils.NewFactory().ToRawKubeConfigLoader().RawConfig()
	if err != nil {
		return CheckInfo{
			FAIL, InfoInvalid, err.Error(), true, err.Error(),
		}
	}
	if len(config.Contexts) == 0 {
		msg := "Please make sure your kubeconfig contains one context at least "
		return CheckInfo{
			FAIL, InfoInvalid, msg, true, msg,
		}
	}
	if contextParam == "" {
		contextParam = config.CurrentContext
	}
	if ctx, ok := config.Contexts[contextParam]; !ok {
		set := sets.NewString()
		for validContext, _ := range config.Contexts {
			set.Insert(validContext)
		}
		msg := fmt.Sprintf(
			"Invalid context '%s',"+
				" you should choose a correct one from below %v",
			contextParam, set.UnsortedList(),
		)
		return CheckInfo{
			FAIL, InfoInvalid, msg, true, msg,
		}
	} else {
		specifiedNs := ctx.Namespace == ""
		err := clientgoutils.DoAuthCheck(
			utils, "", &clientgoutils.AuthChecker{
				Verb:        []string{"list", "get", "watch"},
				ResourceArg: "namespaces",
			},
		)
		if err != nil {
			if errors.Is(err, clientgoutils.PermissionDenied) {
				msg := fmt.Sprintf(
					"Context '%s' can not asscess the cluster scope resources, so you should specify a namespace by using "+
						"'kubectl config set-context %s --namespace=${your_namespace} --kubeconfig=${your_kubeconfig}', or you can add"+
						" a namespace to this context manually. ",
					contextParam, contextParam,
				)
				if specifiedNs{
					return CheckInfo{
						SUCCESS, ctx.Namespace, fmt.Sprintf("Current context can list the resources behind ns %s", ctx.Namespace),
						true, "",
					}
				}
				return CheckInfo{
					FAIL, "Type in a namespace", "Please Type in a correct namespace and try again", true, msg,
				}
			}
			return CheckInfo{
				FAIL, "Type in a namespace", "Please Type in a correct namespace and try again", true, err.Error(),
			}
		}
		return CheckInfo{
			SUCCESS, "Cluster-Scope", "Current context can list with cluster-scope resources", false, "",
		}
	}
}
func CheckKubeconfig ( kubeconfigParams string , contextParam string ) CheckInfo { utils , err := clientgoutils . NewClientGoUtils ( kubeconfigParams , " " ) if err != nil { return CheckInfo { FAIL , InfoInvalid , err . Error ( ) , true , err . Error ( ) , } } config , err := utils . NewFactory ( ) . ToRawKubeConfigLoader ( ) . RawConfig ( ) if err != nil { return CheckInfo { FAIL , InfoInvalid , err . Error ( ) , true , err . Error ( ) , } } if len ( config . Contexts ) == 0 { msg := " " return CheckInfo { FAIL , InfoInvalid , msg , true , msg , } } if contextParam == " " { contextParam = config . CurrentContext } if ctx , ok := config . Contexts [ contextParam ] ; ! ok { set := sets . NewString ( ) for validContext , _ := range config . Contexts { set . Insert ( validContext ) } msg := fmt . Sprintf ( " " + " " , contextParam , set . UnsortedList ( ) , ) return CheckInfo { FAIL , InfoInvalid , msg , true , msg , } } else { specifiedNs := ctx . Namespace == " " err := clientgoutils . DoAuthCheck ( utils , " " , & clientgoutils . AuthChecker { Verb : [ ] string { " " , " " , " " } , ResourceArg : " " , } , ) if err != nil { if errors . Is ( err , clientgoutils . PermissionDenied ) { msg := fmt . Sprintf ( " " + " " + " " , contextParam , contextParam , ) if specifiedNs { return CheckInfo { SUCCESS , ctx . Namespace , fmt . Sprintf ( " " , ctx . Namespace ) , true , " " , } } return CheckInfo { FAIL , " " , " " , true , msg , } } return CheckInfo { FAIL , " " , " " , true , err . Error ( ) , } } return CheckInfo { SUCCESS , " " , " " , false , " " , } } }
CheckKubeconfig return two strings
first is the complate guide
second is a simple msg
CheckKubeconfig return two strings first is the complate guide second is a simple msg
// Success with cluster scope