Skip to content

Commit

Permalink
Refactor ApplyYaml to take in kube clients BOP-1283 (#105)
Browse files Browse the repository at this point in the history
  • Loading branch information
tppolkow authored Dec 19, 2024
1 parent e49275b commit 5dce469
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 15 deletions.
17 changes: 15 additions & 2 deletions pkg/commands/apply.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package commands
import (
"context"
"fmt"
"k8s.io/client-go/rest"
"net"
"time"

Expand All @@ -16,6 +15,9 @@ import (
"github.com/rs/zerolog/log"
"k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/dynamic"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/rest"
)

// Apply installs the Blueprint Operator and applies the components defined in the blueprint
Expand Down Expand Up @@ -100,7 +102,18 @@ func Apply(blueprint *types.Blueprint, kubeConfig *k8s.KubeConfig, providerInsta

log.Info().Msgf("Installing Blueprint Operator")
log.Debug().Msgf("Installing Blueprint Operator using manifest file: %s", blueprint.Spec.Version)
if err = k8s.ApplyYaml(kubeConfig, uri); err != nil {

var client kubernetes.Interface
var dynamicClient dynamic.Interface

if client, err = k8s.GetClient(kubeConfig); err != nil {
return fmt.Errorf("failed to get kubernetes client: %q", err)
}
if dynamicClient, err = k8s.GetDynamicClient(kubeConfig); err != nil {
return fmt.Errorf("failed to get kubernetes dynamic client: %q", err)
}

if err = k8s.ApplyYaml(client, dynamicClient, uri); err != nil {
return fmt.Errorf("failed to install Blueprint Operator: %w", err)
}
} else {
Expand Down
14 changes: 13 additions & 1 deletion pkg/commands/upgrade.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import (
"fmt"

"github.com/rs/zerolog/log"
"k8s.io/client-go/dynamic"
"k8s.io/client-go/kubernetes"

"github.com/mirantiscontainers/blueprint-cli/pkg/distro"
"github.com/mirantiscontainers/blueprint-cli/pkg/k8s"
Expand All @@ -17,8 +19,18 @@ func Upgrade(blueprint *types.Blueprint, kubeConfig *k8s.KubeConfig) error {
return fmt.Errorf("failed to determine operator URI: %w", err)
}

var client kubernetes.Interface
var dynamicClient dynamic.Interface

if client, err = k8s.GetClient(kubeConfig); err != nil {
return fmt.Errorf("failed to get kubernetes client: %q", err)
}
if dynamicClient, err = k8s.GetDynamicClient(kubeConfig); err != nil {
return fmt.Errorf("failed to get kubernetes dynamic client: %q", err)
}

log.Info().Msgf("Upgrading Blueprint Operator using manifest file %q", uri)
if err := k8s.ApplyYaml(kubeConfig, uri); err != nil {
if err := k8s.ApplyYaml(client, dynamicClient, uri); err != nil {
return fmt.Errorf("failed to upgrade blueprint operator: %w", err)
}

Expand Down
14 changes: 2 additions & 12 deletions pkg/k8s/dynamic.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package k8s
import (
"context"
"fmt"

"github.com/rs/zerolog/log"
"k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
Expand All @@ -15,18 +14,9 @@ import (

// ApplyYaml applies a yaml manifest to the cluster from the URI. The URI can be a file path or a URL
// It creates CRDs first and then other objects
// @TODO: Make this function testable by passing a "uri reader" and kubernetes clients
func ApplyYaml(kc *KubeConfig, uri string) error {
// @TODO: Make this function testable by passing a "uri reader"
func ApplyYaml(client kubernetes.Interface, dynamicClient dynamic.Interface, uri string) error {
var err error
var client kubernetes.Interface
var dynamicClient dynamic.Interface

if client, err = GetClient(kc); err != nil {
return fmt.Errorf("failed to get kubernetes client: %q", err)
}
if dynamicClient, err = GetDynamicClient(kc); err != nil {
return fmt.Errorf("failed to get kubernetes dynamic client: %q", err)
}

objs, err := readYamlManifest(uri)
if err != nil {
Expand Down

0 comments on commit 5dce469

Please sign in to comment.