A common chore in automation is to take a command line, like this:
oc get nmstateconfig/cnfde17.redhat.com -n cnfde17 -o yaml
And translate it to Go.
First run the command above and search for the first response lines:
apiVersion: agent-install.openshift.io/v1beta1
kind: NMStateConfig
metadata:
annotations:
In this case you need to start your go code with this import:
import assistedv1beta1 "github.com/openshift/assisted-service/api/v1beta1"
And not with the NMstate import, despite the kind.
For the query you need an object type. In the command line above you queried a spesific namespace: cnfde17, so do this: Now you need to create a typed namespace:
import types "k8s.io/apimachinery/pkg/types"
...
typedNamespace := types.NamespacedName{}
typedNamespace.Name = clusterName
typedNamespace.Namespace = namespace
The query works by supplying an object of the kind of resource you want to query in the namespace. In this case the kind will translate to:
nmStateConfig := assistedv1beta1.NMStateConfig{}
And the query is therefor:
err := Client.Get(context.Background(), typedNamespace, &nmStateConfig)
Here is a nice function with all the above:
// GetNMStateConfig is used to get the NMStateConfig for a specified cluster and namespace.
func GetNMStateConfig(clusterName string, namespace string) (assistedv1beta1.NMStateConfig, error) {
// Create a typed namespace object
typedNamespace := types.NamespacedName{}
typedNamespace.Name = clusterName
typedNamespace.Namespace = namespace
// Create a NMStateConfig object
nmStateConfig := assistedv1beta1.NMStateConfig{}
// Get the NMStateConfig from the hub
err := Client.Get(GetZtpContext(), typedNamespace, &nmStateConfig)
// need to match meta.NoKindMatchError with err.
if err != nil {
return assistedv1beta1.NMStateConfig{}, err
}
return nmStateConfig, nil
}