Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

limit assisted pkg apiClient #339

Merged
merged 1 commit into from
Apr 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 9 additions & 3 deletions pkg/assisted/agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,15 @@ type agentBuilder struct {
Definition *agentInstallV1Beta1.Agent
Object *agentInstallV1Beta1.Agent
errorMsg string
apiClient *clients.Settings
apiClient goclient.Client
}

// AgentAdditionalOptions additional options for agent object.
type AgentAdditionalOptions func(builder *agentBuilder) (*agentBuilder, error)

// newAgentBuilder creates a new instance of agentBuilder
// Users cannot create agent resources themselves as they are generated from the operator.
func newAgentBuilder(apiClient *clients.Settings, definition *agentInstallV1Beta1.Agent) *agentBuilder {
func newAgentBuilder(apiClient goclient.Client, definition *agentInstallV1Beta1.Agent) *agentBuilder {
if definition == nil {
return nil
}
Expand All @@ -55,8 +55,14 @@ func newAgentBuilder(apiClient *clients.Settings, definition *agentInstallV1Beta
func PullAgent(apiClient *clients.Settings, name, nsname string) (*agentBuilder, error) {
glog.V(100).Infof("Pulling existing agent name %s under namespace %s from cluster", name, nsname)

if apiClient == nil {
glog.V(100).Infof("The apiClient cannot be nil")

return nil, fmt.Errorf("the apiClient is nil")
}

builder := agentBuilder{
apiClient: apiClient,
apiClient: apiClient.Client,
Definition: &agentInstallV1Beta1.Agent{
ObjectMeta: metav1.ObjectMeta{
Name: name,
Expand Down
18 changes: 15 additions & 3 deletions pkg/assisted/agentclusterinstall.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ type AgentClusterInstallBuilder struct {
Definition *hiveextV1Beta1.AgentClusterInstall
Object *hiveextV1Beta1.AgentClusterInstall
errorMsg string
apiClient *clients.Settings
apiClient goclient.Client
}

// AgentClusterInstallAdditionalOptions additional options for AgentClusterInstall object.
Expand All @@ -44,8 +44,14 @@ func NewAgentClusterInstallBuilder(
masterCount int,
workerCount int,
network hiveextV1Beta1.Networking) *AgentClusterInstallBuilder {
if apiClient == nil {
glog.V(100).Infof("The apiClient cannot be nil")

return nil
}

builder := AgentClusterInstallBuilder{
apiClient: apiClient,
apiClient: apiClient.Client,
Definition: &hiveextV1Beta1.AgentClusterInstall{
ObjectMeta: metav1.ObjectMeta{
Name: name,
Expand Down Expand Up @@ -487,8 +493,14 @@ func (builder *AgentClusterInstallBuilder) Get() (*hiveextV1Beta1.AgentClusterIn
func PullAgentClusterInstall(apiClient *clients.Settings, name, nsname string) (*AgentClusterInstallBuilder, error) {
glog.V(100).Infof("Pulling existing agentclusterinstall name %s under namespace %s from cluster", name, nsname)

if apiClient == nil {
glog.V(100).Infof("The apiClient cannot be nil")

return nil, fmt.Errorf("the apiClient is nil")
}

builder := AgentClusterInstallBuilder{
apiClient: apiClient,
apiClient: apiClient.Client,
Definition: &hiveextV1Beta1.AgentClusterInstall{
ObjectMeta: metav1.ObjectMeta{
Name: name,
Expand Down
26 changes: 22 additions & 4 deletions pkg/assisted/agentserviceconfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ type AgentServiceConfigBuilder struct {
Definition *agentInstallV1Beta1.AgentServiceConfig
Object *agentInstallV1Beta1.AgentServiceConfig
errorMsg string
apiClient *clients.Settings
apiClient goclient.Client
}

// AgentServiceConfigAdditionalOptions additional options for AgentServiceConfig object.
Expand All @@ -46,8 +46,14 @@ func NewAgentServiceConfigBuilder(
"databaseStorageSpec: %v, filesystemStorageSpec: %v",
databaseStorageSpec, filesystemStorageSpec)

if apiClient == nil {
glog.V(100).Infof("The apiClient cannot be nil")

return nil
}

builder := AgentServiceConfigBuilder{
apiClient: apiClient,
apiClient: apiClient.Client,
Definition: &agentInstallV1Beta1.AgentServiceConfig{
ObjectMeta: metav1.ObjectMeta{
Name: agentServiceConfigName,
Expand All @@ -68,8 +74,14 @@ func NewDefaultAgentServiceConfigBuilder(apiClient *clients.Settings) *AgentServ
glog.V(100).Infof(
"Initializing new agentserviceconfig structure")

if apiClient == nil {
glog.V(100).Infof("The apiClient cannot be nil")

return nil
}

builder := AgentServiceConfigBuilder{
apiClient: apiClient,
apiClient: apiClient.Client,
Definition: &agentInstallV1Beta1.AgentServiceConfig{
ObjectMeta: metav1.ObjectMeta{
Name: agentServiceConfigName,
Expand Down Expand Up @@ -274,8 +286,14 @@ func (builder *AgentServiceConfigBuilder) WaitUntilDeployed(timeout time.Duratio
func PullAgentServiceConfig(apiClient *clients.Settings) (*AgentServiceConfigBuilder, error) {
glog.V(100).Infof("Pulling existing agentserviceconfig name: %s", agentServiceConfigName)

if apiClient == nil {
glog.V(100).Infof("The apiClient cannot be nil")

return nil, fmt.Errorf("the apiClient is nil")
}

builder := AgentServiceConfigBuilder{
apiClient: apiClient,
apiClient: apiClient.Client,
Definition: &agentInstallV1Beta1.AgentServiceConfig{
ObjectMeta: metav1.ObjectMeta{
Name: agentServiceConfigName,
Expand Down
33 changes: 27 additions & 6 deletions pkg/assisted/infraenv.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ type InfraEnvBuilder struct {
Definition *agentInstallV1Beta1.InfraEnv
Object *agentInstallV1Beta1.InfraEnv
errorMsg string
apiClient *clients.Settings
apiClient goclient.Client
}

// InfraEnvAdditionalOptions additional options for InfraEnv object.
Expand All @@ -45,8 +45,14 @@ func NewInfraEnvBuilder(apiClient *clients.Settings, name, nsname, psName string
"name: %s, namespace: %s, pull-secret: %s",
name, nsname, psName)

if apiClient == nil {
glog.V(100).Infof("The apiClient cannot be nil")

return nil
}

builder := InfraEnvBuilder{
apiClient: apiClient,
apiClient: apiClient.Client,
Definition: &agentInstallV1Beta1.InfraEnv{
ObjectMeta: metav1.ObjectMeta{
Name: name,
Expand Down Expand Up @@ -385,9 +391,18 @@ func (builder *InfraEnvBuilder) GetAgentByName(name string) (*agentBuilder, erro
return nil, fmt.Errorf("cannot get agents from non-existent infraenv")
}

agent, err := PullAgent(builder.apiClient, name, builder.Definition.Namespace)
if err != nil {
return nil, err
agent := &agentBuilder{
apiClient: builder.apiClient,
Definition: &agentInstallV1Beta1.Agent{
ObjectMeta: metav1.ObjectMeta{
Name: name,
Namespace: builder.Definition.Namespace,
},
},
}

if !agent.Exists() {
return nil, fmt.Errorf("agent object %s doesn't exist in namespace %s", name, builder.Definition.Namespace)
}

return agent, nil
Expand Down Expand Up @@ -699,8 +714,14 @@ func (builder *InfraEnvBuilder) Get() (*agentInstallV1Beta1.InfraEnv, error) {
func PullInfraEnvInstall(apiClient *clients.Settings, name, nsname string) (*InfraEnvBuilder, error) {
glog.V(100).Infof("Pulling existing infraenv name %s under namespace %s from cluster", name, nsname)

if apiClient == nil {
glog.V(100).Infof("The apiClient cannot be nil")

return nil, fmt.Errorf("the apiClient is nil")
}

builder := InfraEnvBuilder{
apiClient: apiClient,
apiClient: apiClient.Client,
Definition: &agentInstallV1Beta1.InfraEnv{
ObjectMeta: metav1.ObjectMeta{
Name: name,
Expand Down
26 changes: 22 additions & 4 deletions pkg/assisted/nmstateconfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ type NmStateConfigBuilder struct {
// Created NMStateConfig object on the cluster.
Object *assistedv1beta1.NMStateConfig
// API client to interact with the cluster.
apiClient *clients.Settings
apiClient goclient.Client
// errorMsg is processed before NMStateConfig object is created.
errorMsg string
}
Expand All @@ -31,8 +31,14 @@ type NmStateConfigBuilder struct {
func NewNmStateConfigBuilder(apiClient *clients.Settings, name, namespace string) *NmStateConfigBuilder {
glog.V(100).Infof("Initializing new nmstateconfig structure with the name: %s in namespace: %s", name, namespace)

if apiClient == nil {
glog.V(100).Infof("The apiClient cannot be nil")

return nil
}

builder := NmStateConfigBuilder{
apiClient: apiClient,
apiClient: apiClient.Client,
Definition: &assistedv1beta1.NMStateConfig{
ObjectMeta: metav1.ObjectMeta{
Name: name,
Expand Down Expand Up @@ -139,6 +145,12 @@ func (builder *NmStateConfigBuilder) Delete() error {
func ListNmStateConfigsInAllNamespaces(apiClient *clients.Settings) ([]*NmStateConfigBuilder, error) {
nmStateConfigList := &assistedv1beta1.NMStateConfigList{}

if apiClient == nil {
glog.V(100).Infof("The apiClient cannot be nil")

return nil, fmt.Errorf("the apiClient is nil")
}

err := apiClient.List(context.TODO(), nmStateConfigList, &goclient.ListOptions{})

if err != nil {
Expand All @@ -152,7 +164,7 @@ func ListNmStateConfigsInAllNamespaces(apiClient *clients.Settings) ([]*NmStateC
for _, nmStateConfigObj := range nmStateConfigList.Items {
nmStateConf := nmStateConfigObj
nmStateConfBuilder := &NmStateConfigBuilder{
apiClient: apiClient,
apiClient: apiClient.Client,
Definition: &nmStateConf,
Object: &nmStateConf,
}
Expand All @@ -165,6 +177,12 @@ func ListNmStateConfigsInAllNamespaces(apiClient *clients.Settings) ([]*NmStateC

// ListNmStateConfigs returns a NMStateConfig list in a given namespace.
func ListNmStateConfigs(apiClient *clients.Settings, namespace string) ([]*NmStateConfigBuilder, error) {
if apiClient == nil {
glog.V(100).Infof("The apiClient cannot be nil")

return nil, fmt.Errorf("the apiClient is nil")
}

nmStateConfigList := &assistedv1beta1.NMStateConfigList{}

if namespace == "" {
Expand All @@ -185,7 +203,7 @@ func ListNmStateConfigs(apiClient *clients.Settings, namespace string) ([]*NmSta
for _, nmStateConfigObj := range nmStateConfigList.Items {
nmStateConf := nmStateConfigObj
nmStateConfBuilder := &NmStateConfigBuilder{
apiClient: apiClient,
apiClient: apiClient.Client,
Definition: &nmStateConf,
Object: &nmStateConf,
}
Expand Down
Loading