in action/k8s/utils/http.go [49:102]
func GetContextInfo(config *api.Config) (*ContextInfo, error) {
// Get the current context name
currentContext := config.CurrentContext
if currentContext == "" {
return nil, fmt.Errorf("no current context is set")
}
// Get the current context object
context, ok := config.Contexts[currentContext]
if !ok {
return nil, fmt.Errorf("context %s not found", currentContext)
}
// Get the cluster object
cluster, ok := config.Clusters[context.Cluster]
if !ok {
return nil, fmt.Errorf("cluster %s not found", context.Cluster)
}
// Get the user object
authInfo, ok := config.AuthInfos[context.AuthInfo]
if !ok {
return nil, fmt.Errorf("auth info %s not found", context.AuthInfo)
}
// Extract the certificate and API server information
clientCert := authInfo.ClientCertificate
clientKey := authInfo.ClientKey
caCert := cluster.CertificateAuthority
apiServer := cluster.Server
// Content type for API request
contentType := Json
// Check if all required fields are present
if clientCert == "" || clientKey == "" || caCert == "" || apiServer == "" {
missingFields := []string{}
if clientCert == "" {
missingFields = append(missingFields, "client certificate")
}
if clientKey == "" {
missingFields = append(missingFields, "client key")
}
if caCert == "" {
missingFields = append(missingFields, "CA certificate")
}
if apiServer == "" {
missingFields = append(missingFields, "API server")
}
return nil, fmt.Errorf("missing required fields: %s", strings.Join(missingFields, ", "))
}
// Return the ContextInfo struct
return &ContextInfo{
ClientCert: clientCert,
ClientKey: clientKey,
CACert: caCert,
APIServer: apiServer,
ContentType: contentType,
}, nil
}