Skip to content

Commit

Permalink
fix: wait for catalog
Browse files Browse the repository at this point in the history
  • Loading branch information
squakez committed Feb 28, 2023
1 parent 3c80730 commit fc28253
Show file tree
Hide file tree
Showing 8 changed files with 66 additions and 58 deletions.
36 changes: 18 additions & 18 deletions pkg/cmd/completion_bash.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,21 +93,21 @@ __kamel_kubectl_get_servicebinding() {
local service_names
local services_list
local namespace_condition
if command -v awk &> /dev/null ; then
local namespace_config=$(${COMP_WORDS[0]} config --list | awk '/default-namespace/{print $2}')
if [ ! -z $namespace_config ]; then
namespace_condition=$(echo "--namespace ${namespace_config}")
fi
fi
local namespace_flag=$(echo "${flaghash['-n']}${flaghash['--namespace']}")
if [ ! -z $namespace_flag ]; then
namespace_condition=$(echo "--namespace ${namespace_flag}")
fi
template="{{ range .items }}{{ .metadata.name }} {{ end }}"
template_gvkn="{{ range .items }}{{ .kind }}/{{ .apiVersion }}/{{ .metadata.name }} {{ end }}"
template_gvkn="{{ range .items }}{{ .kind }}/{{ .apiVersion }}/{{ .metadata.name }} {{ end }}"
if kubectl_out=$(kubectl get -o template --template="${template}" ${namespace_condition} crd -l service.binding/provisioned-service=true 2>/dev/null); then
kubectl_out="${kubectl_out// /,}"
service_names="${kubectl_out}servicebinding"
Expand All @@ -129,14 +129,14 @@ __kamel_kubectl_get_configmap() {
local template
local kubectl_out
local namespace_condition
if command -v awk &> /dev/null ; then
local namespace_config=$(${COMP_WORDS[0]} config --list | awk '/default-namespace/{print $2}')
if [ ! -z $namespace_config ]; then
namespace_condition=$(echo "--namespace ${namespace_config}")
fi
fi
local namespace_flag=$(echo "${flaghash['-n']}${flaghash['--namespace']}")
if [ ! -z $namespace_flag ]; then
namespace_condition=$(echo "--namespace ${namespace_flag}")
Expand All @@ -153,14 +153,14 @@ __kamel_kubectl_get_secret() {
local template
local kubectl_out
local namespace_condition
if command -v awk &> /dev/null ; then
local namespace_config=$(${COMP_WORDS[0]} config --list | awk '/default-namespace/{print $2}')
if [ ! -z $namespace_config ]; then
namespace_condition=$(echo "--namespace ${namespace_config}")
fi
fi
local namespace_flag=$(echo "${flaghash['-n']}${flaghash['--namespace']}")
if [ ! -z $namespace_flag ]; then
namespace_condition=$(echo "--namespace ${namespace_flag}")
Expand All @@ -177,14 +177,14 @@ __kamel_kubectl_get_integrations() {
local template
local kubectl_out
local namespace_condition
if command -v awk &> /dev/null ; then
local namespace_config=$(${COMP_WORDS[0]} config --list | awk '/default-namespace/{print $2}')
if [ ! -z $namespace_config ]; then
namespace_condition=$(echo "--namespace ${namespace_config}")
fi
fi
local namespace_flag=$(echo "${flaghash['-n']}${flaghash['--namespace']}")
if [ ! -z $namespace_flag ]; then
namespace_condition=$(echo "--namespace ${namespace_flag}")
Expand All @@ -201,14 +201,14 @@ __kamel_kubectl_get_integrationkits() {
local template
local kubectl_out
local namespace_condition
if command -v awk &> /dev/null ; then
local namespace_config=$(${COMP_WORDS[0]} config --list | awk '/default-namespace/{print $2}')
if [ ! -z $namespace_config ]; then
namespace_condition=$(echo "--namespace ${namespace_config}")
fi
fi
local namespace_flag=$(echo "${flaghash['-n']}${flaghash['--namespace']}")
if [ ! -z $namespace_flag ]; then
namespace_condition=$(echo "--namespace ${namespace_flag}")
Expand All @@ -225,14 +225,14 @@ __kamel_kubectl_get_non_platform_integrationkits() {
local template
local kubectl_out
local namespace_condition
if command -v awk &> /dev/null ; then
local namespace_config=$(${COMP_WORDS[0]} config --list | awk '/default-namespace/{print $2}')
if [ ! -z $namespace_config ]; then
namespace_condition=$(echo "--namespace ${namespace_config}")
fi
fi
local namespace_flag=$(echo "${flaghash['-n']}${flaghash['--namespace']}")
if [ ! -z $namespace_flag ]; then
namespace_condition=$(echo "--namespace ${namespace_flag}")
Expand All @@ -250,14 +250,14 @@ __kamel_kubectl_get_kamelets() {
local template
local kubectl_out
local namespace_condition
if command -v awk &> /dev/null ; then
local namespace_config=$(${COMP_WORDS[0]} config --list | awk '/default-namespace/{print $2}')
if [ ! -z $namespace_config ]; then
namespace_condition=$(echo "--namespace ${namespace_config}")
fi
fi
local namespace_flag=$(echo "${flaghash['-n']}${flaghash['--namespace']}")
if [ ! -z $namespace_flag ]; then
namespace_condition=$(echo "--namespace ${namespace_flag}")
Expand All @@ -274,14 +274,14 @@ __kamel_kubectl_get_non_bundled_non_readonly_kamelets() {
local template
local kubectl_out
local namespace_condition
if command -v awk &> /dev/null ; then
local namespace_config=$(${COMP_WORDS[0]} config --list | awk '/default-namespace/{print $2}')
if [ ! -z $namespace_config ]; then
namespace_condition=$(echo "--namespace ${namespace_config}")
fi
fi
local namespace_flag=$(echo "${flaghash['-n']}${flaghash['--namespace']}")
if [ ! -z $namespace_flag ]; then
namespace_condition=$(echo "--namespace ${namespace_flag}")
Expand Down Expand Up @@ -430,7 +430,7 @@ func configureBashAnnotationForFlag(command *cobra.Command, flagName string, ann
func computeCamelDependencies() string {
catalog, err := camel.DefaultCatalog()
if err != nil || catalog == nil {
catalog = camel.NewRuntimeCatalog(v1.CamelCatalog{}.Spec)
catalog = camel.NewRuntimeCatalog(v1.CamelCatalog{})
}

results := make([]string, 0, len(catalog.Artifacts)+len(catalog.Loaders))
Expand Down
15 changes: 7 additions & 8 deletions pkg/controller/catalog/initialize.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ import (
"github.com/apache/camel-k/pkg/client"
platformutil "github.com/apache/camel-k/pkg/platform"
"github.com/apache/camel-k/pkg/util"
"github.com/apache/camel-k/pkg/util/log"
spectrum "github.com/container-tools/spectrum/pkg/builder"
gcrv1 "github.com/google/go-containerregistry/pkg/v1"
corev1 "k8s.io/api/core/v1"
Expand Down Expand Up @@ -89,7 +88,7 @@ func initialize(options spectrum.Options, registryAddress string, catalog *v1.Ca

if pipeErr != nil {
// In the unlikely case of an error, use stdout instead of aborting
log.Errorf(pipeErr, "Unable to remap I/O. Spectrum messages will be displayed on the stdout")
Log.Errorf(pipeErr, "Unable to remap I/O. Spectrum messages will be displayed on the stdout")
newStdW = os.Stdout
}
go readSpectrumLogs(newStdR)
Expand Down Expand Up @@ -142,19 +141,19 @@ func initialize(options spectrum.Options, registryAddress string, catalog *v1.Ca
}

func imageExists(options spectrum.Options) bool {
log.Infof("Checking if Camel K builder container %s already exists...", options.Base)
Log.Infof("Checking if Camel K builder container %s already exists...", options.Base)
ctrImg, err := spectrum.Pull(options)
if ctrImg != nil && err == nil {
var hash gcrv1.Hash
if hash, err = ctrImg.Digest(); err != nil {
log.Errorf(err, "Cannot calculate digest")
Log.Errorf(err, "Cannot calculate digest")
return false
}
log.Infof("Camel K builder container with digest %s", hash.String())
Log.Infof("found Camel K builder container with digest %s", hash.String())
return true
}

log.Errorf(err, "Couldn't pull image")
Log.Errorf(err, "Couldn't pull image")
return false
}

Expand All @@ -164,7 +163,7 @@ func buildRuntimeBuilderImage(options spectrum.Options) error {
if options.Base == "" {
return fmt.Errorf("Missing base image, likely catalog is not compatible with this Camel K version")
}
log.Infof("Making up Camel K builder container %s", options.Target)
Log.Infof("Making up Camel K builder container %s", options.Target)

if jobs := runtime.GOMAXPROCS(0); jobs > 1 {
options.Jobs = jobs
Expand All @@ -186,7 +185,7 @@ func readSpectrumLogs(newStdOut io.Reader) {

for scanner.Scan() {
line := scanner.Text()
log.Infof(line)
Log.Infof(line)
}
}

Expand Down
25 changes: 1 addition & 24 deletions pkg/controller/integrationkit/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,30 +87,6 @@ func (action *buildAction) handleBuildSubmitted(ctx context.Context, kit *v1.Int
return nil, errors.New("undefined camel catalog")
}

catalog, err := kubernetes.GetCamelCatalog(
ctx,
action.client,
fmt.Sprintf("camel-catalog-%s-quarkus", env.CamelCatalog.Runtime.Version),
kit.Namespace,
)
if err != nil {
return nil, err
}

if catalog.Status.Phase == v1.CamelCatalogPhaseError {
kit.Status.Phase = v1.IntegrationKitPhaseError
kit.Status.SetErrorCondition(
v1.IntegrationKitConditionCatalogAvailable,
fmt.Sprintf("Camel Catalog %s error", catalog.Spec.Runtime.Version),
errors.Errorf("%s", catalog.Status.GetCondition(v1.CamelCatalogConditionReady).Reason),
)
return kit, nil
}
if catalog.Status.Phase != v1.CamelCatalogPhaseReady {
kit.Status.Phase = v1.IntegrationKitPhaseWaitingForCatalog
return kit, nil
}

labels := kubernetes.FilterCamelCreatorLabels(kit.Labels)
labels[v1.IntegrationKitLayoutLabel] = kit.Labels[v1.IntegrationKitLayoutLabel]

Expand All @@ -137,6 +113,7 @@ func (action *buildAction) handleBuildSubmitted(ctx context.Context, kit *v1.Int

if buildStrategy == v1.BuildStrategyPod {
// We must ensure the expected service account is available or create it
// TODO make it singleton
err = platform.CreateBuilderServiceAccount(env.Ctx, env.Client, env.Platform)
if err != nil {
return nil, errors.Wrap(err, "Error while creating Camel K Builder service account")
Expand Down
33 changes: 32 additions & 1 deletion pkg/controller/integrationkit/initialize.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,14 @@ package integrationkit

import (
"context"
"fmt"
"strings"

v1 "github.com/apache/camel-k/pkg/apis/camel/v1"
"github.com/apache/camel-k/pkg/trait"
"github.com/apache/camel-k/pkg/util/defaults"
"github.com/apache/camel-k/pkg/util/kubernetes"
"github.com/pkg/errors"
)

// NewInitializeAction creates a new initialization handling action for the kit.
Expand All @@ -43,7 +47,7 @@ func (action *initializeAction) CanHandle(kit *v1.IntegrationKit) bool {
}

func (action *initializeAction) Handle(ctx context.Context, kit *v1.IntegrationKit) (*v1.IntegrationKit, error) {
_, err := trait.Apply(ctx, action.client, nil, kit)
env, err := trait.Apply(ctx, action.client, nil, kit)
if err != nil {
return nil, err
}
Expand All @@ -52,6 +56,33 @@ func (action *initializeAction) Handle(ctx context.Context, kit *v1.IntegrationK
// by default the kit should be built
kit.Status.Phase = v1.IntegrationKitPhaseBuildSubmitted
} else {

// Wait for CamelCatalog to be ready
catalog, err := kubernetes.GetCamelCatalog(
ctx,
action.client,
fmt.Sprintf("camel-catalog-%s", strings.ToLower(env.RuntimeVersion)),
kit.Namespace,
)
if err != nil {
Log.Infof("Camel Catalog - could not retrieve catalog %s", err.Error())
return nil, err
}

if catalog.Status.Phase == v1.CamelCatalogPhaseError {
kit.Status.Phase = v1.IntegrationKitPhaseError
kit.Status.SetErrorCondition(
v1.IntegrationKitConditionCatalogAvailable,
fmt.Sprintf("Camel Catalog %s error", catalog.Spec.Runtime.Version),
errors.Errorf("%s", catalog.Status.GetCondition(v1.CamelCatalogConditionReady).Reason),
)
return kit, nil
}
if catalog.Status.Phase != v1.CamelCatalogPhaseReady {
kit.Status.Phase = v1.IntegrationKitPhaseWaitingForCatalog
return kit, nil
}

// but in case it has been created from an image, mark the
// kit as ready
kit.Status.Phase = v1.IntegrationKitPhaseReady
Expand Down
2 changes: 1 addition & 1 deletion pkg/controller/integrationkit/integrationkit_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,7 @@ func (r *reconcileIntegrationKit) Reconcile(ctx context.Context, request reconci
NewErrorAction(),
}

var targetPhase v1.IntegrationKitPhase
targetPhase := instance.Status.Phase

for _, a := range actions {
a.InjectClient(r.client)
Expand Down
7 changes: 4 additions & 3 deletions pkg/util/camel/camel_runtime_catalog.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,11 @@ import (
v1 "github.com/apache/camel-k/pkg/apis/camel/v1"
)

// NewRuntimeCatalog creates a runtime catalog with the given catalog spec.
func NewRuntimeCatalog(spec v1.CamelCatalogSpec) *RuntimeCatalog {
// NewRuntimeCatalog creates a runtime catalog with the given catalog.
func NewRuntimeCatalog(cat v1.CamelCatalog) *RuntimeCatalog {
catalog := RuntimeCatalog{}
catalog.CamelCatalogSpec = spec
catalog.CamelCatalogSpec = cat.Spec
catalog.CamelCatalogStatus = cat.Status
catalog.artifactByScheme = make(map[string]string)
catalog.artifactByDataFormat = make(map[string]string)
catalog.schemesByID = make(map[string]v1.CamelScheme)
Expand Down
4 changes: 2 additions & 2 deletions pkg/util/camel/camel_util.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ var (
func findBestMatch(catalogs []v1.CamelCatalog, runtime v1.RuntimeSpec) (*RuntimeCatalog, error) {
for _, catalog := range catalogs {
if catalog.Spec.Runtime.Version == runtime.Version && catalog.Spec.Runtime.Provider == runtime.Provider {
return NewRuntimeCatalog(catalog.Spec), nil
return NewRuntimeCatalog(catalog), nil
}
}

Expand All @@ -54,7 +54,7 @@ func findBestMatch(catalogs []v1.CamelCatalog, runtime v1.RuntimeSpec) (*Runtime
cc := newCatalogVersionCollection(catalogs)
for _, c := range cc {
if rc.Check(c.RuntimeVersion) {
return NewRuntimeCatalog(c.Catalog.Spec), nil
return NewRuntimeCatalog(*c.Catalog), nil
}
}

Expand Down
2 changes: 1 addition & 1 deletion pkg/util/camel/catalog.go
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ func GenerateCatalogCommon(
return yaml2.Unmarshal(content, &catalog)
})

return NewRuntimeCatalog(catalog.Spec), err
return NewRuntimeCatalog(catalog), err
}

func generateMavenProject(runtimeVersion string, providerDependencies []maven.Dependency) maven.Project {
Expand Down

0 comments on commit fc28253

Please sign in to comment.