Skip to content
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
5 changes: 2 additions & 3 deletions cmd/rhoas/main.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package main

import (
"context"
"fmt"
"os"
"strings"
Expand Down Expand Up @@ -52,12 +51,12 @@ func main() {
err = rootCmd.Execute()
if err == nil {
if debug.Enabled() {
build.CheckForUpdate(context.Background(), cmdFactory.Logger, localizer)
build.CheckForUpdate(cmdFactory.Context, cmdFactory.Logger, localizer)
}
return
}
cmdFactory.Logger.Error(rootError(err, localizer))
build.CheckForUpdate(context.Background(), cmdFactory.Logger, localizer)
build.CheckForUpdate(cmdFactory.Context, cmdFactory.Logger, localizer)
os.Exit(1)
}

Expand Down
4 changes: 2 additions & 2 deletions pkg/ams/ams.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ import (
"github.com/redhat-developer/app-services-cli/pkg/connection"
)

func CheckTermsAccepted(conn connection.Connection) (accepted bool, redirectURI string, err error) {
func CheckTermsAccepted(ctx context.Context, conn connection.Connection) (accepted bool, redirectURI string, err error) {
termsReview, _, err := conn.API().AccountMgmt().
ApiAuthorizationsV1SelfTermsReviewPost(context.Background()).
ApiAuthorizationsV1SelfTermsReviewPost(ctx).
SelfTermsReview(amsclient.SelfTermsReview{
EventCode: &build.TermsReviewEventCode,
SiteCode: &build.TermsReviewSiteCode,
Expand Down
4 changes: 2 additions & 2 deletions pkg/cluster/kcManager.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,10 +84,10 @@ func getKafkaConnectionsAPIURL(namespace string) string {
return fmt.Sprintf("/apis/rhoas.redhat.com/v1alpha1/namespaces/%v/kafkaconnections", namespace)
}

func watchForKafkaStatus(c *KubernetesCluster, crName string, namespace string) error {
func watchForKafkaStatus(ctx context.Context, c *KubernetesCluster, crName string, namespace string) error {
c.logger.Info(c.localizer.MustLocalize("cluster.kubernetes.watchForKafkaStatus.log.info.wait"))

w, err := c.dynamicClient.Resource(AKCResource).Namespace(namespace).Watch(context.TODO(), metav1.ListOptions{
w, err := c.dynamicClient.Resource(AKCResource).Namespace(namespace).Watch(ctx, metav1.ListOptions{
FieldSelector: fields.OneTermEqualSelector("metadata.name", crName).String(),
})
if err != nil {
Expand Down
8 changes: 4 additions & 4 deletions pkg/cluster/kubernetes_cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ func (c *KubernetesCluster) createKafkaConnectionCustomResource(ctx context.Cont

c.logger.Info(c.localizer.MustLocalize("cluster.kubernetes.createKafkaCR.log.info.customResourceCreated", localize.NewEntry("Name", crName)))

return watchForKafkaStatus(c, crName, namespace)
return watchForKafkaStatus(ctx, c, crName, namespace)
}

// IsRhoasOperatorAvailableOnCluster checks the cluster to see if a KafkaConnection CRD is installed
Expand All @@ -222,7 +222,7 @@ func (c *KubernetesCluster) IsRhoasOperatorAvailableOnCluster(ctx context.Contex
}

func (c *KubernetesCluster) createTokenSecretIfNeeded(ctx context.Context, namespace string, opts *ConnectArguments) error {
_, err := c.clientset.CoreV1().Secrets(namespace).Get(context.TODO(), tokenSecretName, metav1.GetOptions{})
_, err := c.clientset.CoreV1().Secrets(namespace).Get(ctx, tokenSecretName, metav1.GetOptions{})
if err == nil {
c.logger.Info(c.localizer.MustLocalize("cluster.kubernetes.tokensecret.log.info.found"), tokenSecretName)
return nil
Expand Down Expand Up @@ -271,7 +271,7 @@ func (c *KubernetesCluster) createTokenSecretIfNeeded(ctx context.Context, names

// createSecret creates a new secret to store the SASL/PLAIN credentials from the service account
func (c *KubernetesCluster) createServiceAccountSecretIfNeeded(ctx context.Context, namespace string) error {
_, err := c.clientset.CoreV1().Secrets(namespace).Get(context.TODO(), serviceAccountSecretName, metav1.GetOptions{})
_, err := c.clientset.CoreV1().Secrets(namespace).Get(ctx, serviceAccountSecretName, metav1.GetOptions{})
if err == nil {
c.logger.Info(c.localizer.MustLocalize("cluster.kubernetes.serviceaccountsecret.log.info.exist"))
return nil
Expand All @@ -297,7 +297,7 @@ func (c *KubernetesCluster) createServiceAccountSecretIfNeeded(ctx context.Conte
},
}

createdSecret, err := c.clientset.CoreV1().Secrets(namespace).Create(context.TODO(), secret, metav1.CreateOptions{})
createdSecret, err := c.clientset.CoreV1().Secrets(namespace).Create(ctx, secret, metav1.CreateOptions{})
if err != nil {
return fmt.Errorf("%v: %w", c.localizer.MustLocalize("cluster.kubernetes.serviceaccountsecret.error.createError"), err)
}
Expand Down
24 changes: 12 additions & 12 deletions pkg/cluster/serviceBinding.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ type ServiceBindingOptions struct {
BindAsFiles bool
}

func ExecuteServiceBinding(logger logging.Logger, localizer localize.Localizer, options *ServiceBindingOptions) error {
func ExecuteServiceBinding(ctx context.Context, logger logging.Logger, localizer localize.Localizer, options *ServiceBindingOptions) error {
clients, err := client(localizer)
if err != nil {
return err
Expand All @@ -62,12 +62,12 @@ func ExecuteServiceBinding(logger logging.Logger, localizer localize.Localizer,

// Get proper deployment
if options.AppName == "" {
options.AppName, err = fetchAppNameFromCluster(clients, localizer, ns)
options.AppName, err = fetchAppNameFromCluster(ctx, clients, localizer, ns)
if err != nil {
return err
}
} else {
_, err = clients.dynamicClient.Resource(deploymentResource).Namespace(ns).Get(context.TODO(), options.AppName, metav1.GetOptions{})
_, err = clients.dynamicClient.Resource(deploymentResource).Namespace(ns).Get(ctx, options.AppName, metav1.GetOptions{})
if err != nil {
return err
}
Expand All @@ -92,13 +92,13 @@ func ExecuteServiceBinding(logger logging.Logger, localizer localize.Localizer,
}

// Check KafkaConnection
_, err = clients.dynamicClient.Resource(AKCResource).Namespace(ns).Get(context.TODO(), options.ServiceName, metav1.GetOptions{})
_, err = clients.dynamicClient.Resource(AKCResource).Namespace(ns).Get(ctx, options.ServiceName, metav1.GetOptions{})
if err != nil {
return errors.New(localizer.MustLocalize("cluster.serviceBinding.serviceMissing.message"))
}

// Execute binding
err = performBinding(options, ns, clients, logger, localizer)
err = performBinding(ctx, options, ns, clients, logger, localizer)
if err != nil {
return err
}
Expand All @@ -107,7 +107,7 @@ func ExecuteServiceBinding(logger logging.Logger, localizer localize.Localizer,
return nil
}

func performBinding(options *ServiceBindingOptions, ns string, clients *KubernetesClients, logger logging.Logger, localizer localize.Localizer) error {
func performBinding(ctx context.Context, options *ServiceBindingOptions, ns string, clients *KubernetesClients, logger logging.Logger, localizer localize.Localizer) error {
serviceRef := v1alpha1.Service{
NamespacedRef: v1alpha1.NamespacedRef{
Ref: v1alpha1.Ref{
Expand Down Expand Up @@ -156,7 +156,7 @@ func performBinding(options *ServiceBindingOptions, ns string, clients *Kubernet

// Check of operator is installed
_, err := clients.dynamicClient.Resource(v1alpha1.GroupVersionResource).Namespace(ns).
List(context.TODO(), metav1.ListOptions{Limit: 1})
List(ctx, metav1.ListOptions{Limit: 1})
if err != nil {
if options.ForceUseOperator {
return errors.New(localizer.MustLocalize("cluster.serviceBinding.operatorMissing") + err.Error())
Expand All @@ -165,10 +165,10 @@ func performBinding(options *ServiceBindingOptions, ns string, clients *Kubernet
return useSDKForBinding(clients, sb)
}

return useOperatorForBinding(logger, localizer, sb, clients, ns)
return useOperatorForBinding(ctx, logger, localizer, sb, clients, ns)
}

func useOperatorForBinding(logger logging.Logger, localizer localize.Localizer, sb *v1alpha1.ServiceBinding, clients *KubernetesClients, ns string) error {
func useOperatorForBinding(ctx context.Context, logger logging.Logger, localizer localize.Localizer, sb *v1alpha1.ServiceBinding, clients *KubernetesClients, ns string) error {
logger.Info(localizer.MustLocalize("cluster.serviceBinding.usingOperator"))
sbData, err := runtime.DefaultUnstructuredConverter.ToUnstructured(sb)
if err != nil {
Expand All @@ -177,7 +177,7 @@ func useOperatorForBinding(logger logging.Logger, localizer localize.Localizer,

unstructuredSB := unstructured.Unstructured{Object: sbData}
_, err = clients.dynamicClient.Resource(v1alpha1.GroupVersionResource).Namespace(ns).
Create(context.TODO(), &unstructuredSB, metav1.CreateOptions{})
Create(ctx, &unstructuredSB, metav1.CreateOptions{})

return err
}
Expand All @@ -199,8 +199,8 @@ func useSDKForBinding(clients *KubernetesClients, sb *v1alpha1.ServiceBinding) e
return err
}

func fetchAppNameFromCluster(clients *KubernetesClients, localizer localize.Localizer, ns string) (string, error) {
list, err := clients.dynamicClient.Resource(deploymentResource).Namespace(ns).List(context.TODO(), metav1.ListOptions{})
func fetchAppNameFromCluster(ctx context.Context, clients *KubernetesClients, localizer localize.Localizer, ns string) (string, error) {
list, err := clients.dynamicClient.Resource(deploymentResource).Namespace(ns).List(ctx, metav1.ListOptions{})
if err != nil {
return "", err
}
Expand Down
8 changes: 5 additions & 3 deletions pkg/cmd/cluster/bind/bind.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ type Options struct {
Logger logging.Logger
IO *iostreams.IOStreams
localizer localize.Localizer
Context context.Context

kubeconfigLocation string
namespace string
Expand All @@ -42,6 +43,7 @@ func NewBindCommand(f *factory.Factory) *cobra.Command {
Logger: f.Logger,
IO: f.IOStreams,
localizer: f.Localizer,
Context: f.Context,
}

cmd := &cobra.Command{
Expand Down Expand Up @@ -85,7 +87,7 @@ func runBind(opts *Options) error {
// In future config will include Id's of other services
if cfg.Services.Kafka == nil || opts.ignoreContext {
// nolint:govet
selectedKafka, err := kafka.InteractiveSelect(apiConnection, opts.Logger)
selectedKafka, err := kafka.InteractiveSelect(opts.Context, apiConnection, opts.Logger)
if err != nil {
return err
}
Expand All @@ -98,7 +100,7 @@ func runBind(opts *Options) error {
}

api := apiConnection.API()
kafkaInstance, _, err := api.Kafka().GetKafkaById(context.Background(), opts.selectedKafka).Execute()
kafkaInstance, _, err := api.Kafka().GetKafkaById(opts.Context, opts.selectedKafka).Execute()
if err != nil {
return err
}
Expand All @@ -107,7 +109,7 @@ func runBind(opts *Options) error {
return errors.New(opts.localizer.MustLocalize("cluster.bind.error.emptyResponse"))
}

err = cluster.ExecuteServiceBinding(opts.Logger, opts.localizer, &cluster.ServiceBindingOptions{
err = cluster.ExecuteServiceBinding(opts.Context, opts.Logger, opts.localizer, &cluster.ServiceBindingOptions{
ServiceName: kafkaInstance.GetName(),
Namespace: opts.namespace,
AppName: opts.appName,
Expand Down
6 changes: 4 additions & 2 deletions pkg/cmd/cluster/connect/connect.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ type Options struct {
Logger logging.Logger
IO *iostreams.IOStreams
localizer localize.Localizer
Context context.Context

kubeconfigLocation string
namespace string
Expand All @@ -39,6 +40,7 @@ func NewConnectCommand(f *factory.Factory) *cobra.Command {
Logger: f.Logger,
IO: f.IOStreams,
localizer: f.Localizer,
Context: f.Context,
}

cmd := &cobra.Command{
Expand Down Expand Up @@ -83,7 +85,7 @@ func runConnect(opts *Options) error {
// In future config will include Id's of other services
if cfg.Services.Kafka == nil || opts.ignoreContext {
// nolint
selectedKafka, err := kafka.InteractiveSelect(conn, opts.Logger)
selectedKafka, err := kafka.InteractiveSelect(opts.Context, conn, opts.Logger)
if err != nil {
return err
}
Expand All @@ -103,7 +105,7 @@ func runConnect(opts *Options) error {
Namespace: opts.namespace,
}

err = clusterConn.Connect(context.Background(), arguments)
err = clusterConn.Connect(opts.Context, arguments)
if err != nil {
return err
}
Expand Down
5 changes: 3 additions & 2 deletions pkg/cmd/cluster/status/status.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ type Options struct {
Logger logging.Logger
IO *iostreams.IOStreams
localizer localize.Localizer

Context context.Context
kubeconfig string
}

Expand All @@ -37,6 +37,7 @@ func NewStatusCommand(f *factory.Factory) *cobra.Command {
Logger: f.Logger,
IO: f.IOStreams,
localizer: f.Localizer,
Context: f.Context,
}

cmd := &cobra.Command{
Expand Down Expand Up @@ -68,7 +69,7 @@ func runStatus(opts *Options) error {

var operatorStatus string
// Add versioning in future
isCRDInstalled, err := clusterConn.IsRhoasOperatorAvailableOnCluster(context.Background())
isCRDInstalled, err := clusterConn.IsRhoasOperatorAvailableOnCluster(opts.Context)
if isCRDInstalled && err != nil {
opts.Logger.Debug(err)
}
Expand Down
5 changes: 4 additions & 1 deletion pkg/cmd/factory/default.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ func New(localizer localize.Localizer) *Factory {

logger, _ = loggerBuilder.Build()

ctx := context.Background()

connectionFunc := func(connectionCfg *connection.Config) (connection.Connection, error) {
if conn != nil {
return conn, nil
Expand Down Expand Up @@ -99,7 +101,7 @@ func New(localizer localize.Localizer) *Factory {
return nil, err
}

err = conn.RefreshTokens(context.TODO())
err = conn.RefreshTokens(ctx)
if err != nil {
return nil, err
}
Expand All @@ -113,5 +115,6 @@ func New(localizer localize.Localizer) *Factory {
Connection: connectionFunc,
Logger: logger,
Localizer: localizer,
Context: ctx,
}
}
3 changes: 3 additions & 0 deletions pkg/cmd/factory/factory.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package factory

import (
"context"
"github.com/redhat-developer/app-services-cli/internal/config"
"github.com/redhat-developer/app-services-cli/pkg/connection"
"github.com/redhat-developer/app-services-cli/pkg/iostreams"
Expand All @@ -21,6 +22,8 @@ type Factory struct {
Logger logging.Logger
// Localizer provides text to the commands
Localizer localize.Localizer
// Context returns the default context for the application
Context context.Context
}

type ConnectionFunc func(cfg *connection.Config) (connection.Connection, error)
8 changes: 4 additions & 4 deletions pkg/cmd/kafka/consumergroup/delete/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ type Options struct {
Connection factory.ConnectionFunc
Logger logging.Logger
localizer localize.Localizer
Context context.Context
}

// NewDeleteConsumerGroupCommand gets a new command for deleting a consumer group.
Expand All @@ -36,6 +37,7 @@ func NewDeleteConsumerGroupCommand(f *factory.Factory) *cobra.Command {
IO: f.IOStreams,
Logger: f.Logger,
localizer: f.Localizer,
Context: f.Context,
}

cmd := &cobra.Command{
Expand Down Expand Up @@ -88,9 +90,7 @@ func runCmd(opts *Options) error {
return err
}

ctx := context.Background()

_, httpRes, err := api.GroupsApi.GetConsumerGroupById(ctx, opts.id).Execute()
_, httpRes, err := api.GroupsApi.GetConsumerGroupById(opts.Context, opts.id).Execute()
if httpRes != nil {
defer httpRes.Body.Close()
}
Expand Down Expand Up @@ -122,7 +122,7 @@ func runCmd(opts *Options) error {
}
}

httpRes, err = api.GroupsApi.DeleteConsumerGroupById(ctx, opts.id).Execute()
httpRes, err = api.GroupsApi.DeleteConsumerGroupById(opts.Context, opts.id).Execute()
if httpRes != nil {
defer httpRes.Body.Close()
}
Expand Down
6 changes: 3 additions & 3 deletions pkg/cmd/kafka/consumergroup/describe/describe.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ type Options struct {
Config config.IConfig
Connection factory.ConnectionFunc
localizer localize.Localizer
Context context.Context
}

type consumerRow struct {
Expand All @@ -53,6 +54,7 @@ func NewDescribeConsumerGroupCommand(f *factory.Factory) *cobra.Command {
Config: f.Config,
IO: f.IOStreams,
localizer: f.Localizer,
Context: f.Context,
}
cmd := &cobra.Command{
Use: "describe",
Expand Down Expand Up @@ -111,9 +113,7 @@ func runCmd(opts *Options) error {
return err
}

ctx := context.Background()

consumerGroupData, httpRes, err := api.GroupsApi.GetConsumerGroupById(ctx, opts.id).Execute()
consumerGroupData, httpRes, err := api.GroupsApi.GetConsumerGroupById(opts.Context, opts.id).Execute()
if httpRes != nil {
defer httpRes.Body.Close()
}
Expand Down
Loading