Skip to content

Commit

Permalink
address review comments related to inconsistent error/log messages
Browse files Browse the repository at this point in the history
  • Loading branch information
kale-amruta committed Feb 28, 2023
1 parent e1fc40c commit e4608b2
Show file tree
Hide file tree
Showing 9 changed files with 114 additions and 89 deletions.
33 changes: 15 additions & 18 deletions cmd/reposervercontroller/main.go
Original file line number Diff line number Diff line change
@@ -1,18 +1,16 @@
/*
Copyright 2022.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
// Copyright 2023 The Kanister Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package main

Expand All @@ -23,11 +21,10 @@ import (
// Import all Kubernetes client auth plugins (e.g. Azure, GCP, OIDC, etc.)
// to ensure that exec-entrypoint and run can make use of them.
"go.uber.org/zap/zapcore"
_ "k8s.io/client-go/plugin/pkg/client/auth"

"k8s.io/apimachinery/pkg/runtime"
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
clientgoscheme "k8s.io/client-go/kubernetes/scheme"
_ "k8s.io/client-go/plugin/pkg/client/auth"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/healthz"
"sigs.k8s.io/controller-runtime/pkg/log/zap"
Expand All @@ -41,7 +38,7 @@ import (
var (
scheme = runtime.NewScheme()
setupLog = ctrl.Log.WithName("setup")
defaultLogLevel = zapcore.ErrorLevel
defaultLogLevel = zapcore.InfoLevel
)

func init() {
Expand Down
2 changes: 1 addition & 1 deletion pkg/consts/consts.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,4 @@ const (
)

const LatestKanisterToolsImage = "ghcr.io/kanisterio/kanister-tools:v9.99.9-dev"
const KanisterToolsImage = "ghcr.io/kanisterio/kanister-tools:0.88.0"
const KanisterToolsImage = "ghcr.io/kanisterio/kanister-tools:0.89.0"
51 changes: 25 additions & 26 deletions pkg/controllers/repositoryserver/handler.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2022 The Kanister Authors.
// Copyright 2023 The Kanister Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand All @@ -23,6 +23,7 @@ import (

"github.com/go-logr/logr"
"github.com/jpillora/backoff"
"github.com/kanisterio/kanister/pkg/kopia/command/storage"
"github.com/pkg/errors"
corev1 "k8s.io/api/core/v1"
apierrors "k8s.io/apimachinery/pkg/api/errors"
Expand All @@ -31,8 +32,6 @@ import (
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"

"github.com/kanisterio/kanister/pkg/kopia/command/storage"

crv1alpha1 "github.com/kanisterio/kanister/pkg/apis/cr/v1alpha1"
"github.com/kanisterio/kanister/pkg/kube"
"github.com/kanisterio/kanister/pkg/poll"
Expand All @@ -50,28 +49,28 @@ type RepoServerHandler struct {
func (h *RepoServerHandler) CreateOrUpdateOwnedResources(ctx context.Context) error {
svc, err := h.reconcileService(ctx)
if err != nil {
return errors.Wrap(err, "failed to reconcile service")
return errors.Wrap(err, "Failed to reconcile service")
}
if err = h.getSecretsFromCR(ctx); err != nil {
return errors.Wrap(err, "failed to get repository server secrets")
return errors.Wrap(err, "Failed to get Kopia API server secrets")
}
pod, err := h.reconcilePod(ctx, svc)
if err != nil {
return errors.Wrap(err, "failed to reconcile repository server pod")
return errors.Wrap(err, "Failed to reconcile Kopia API server pod")
}
if err := h.waitForPodReady(ctx, pod); err != nil {
return errors.Wrap(err, "repository server pod not in ready state")
return errors.Wrap(err, "Kopia API server pod not in ready state")
}
if err := h.connectToKopiaRepository(); err != nil {
return errors.Wrap(err, "failed to connect to kopia repository")
return errors.Wrap(err, "Failed to connect to Kopia repository")
}

if err := h.startRepoProxyServer(ctx); err != nil {
return errors.Wrap(err, "failed to start kopia repository servver")
return errors.Wrap(err, "Failed to start Kopia API server")
}

if err := h.createOrUpdateClientUsers(ctx); err != nil {
return errors.Wrap(err, "failed to create/update kopia server access users")
return errors.Wrap(err, "Failed to create/update kopia API server access users")
}
return nil
}
Expand All @@ -80,7 +79,7 @@ func (h *RepoServerHandler) reconcileService(ctx context.Context) (*corev1.Servi
repoServerNamespace := h.RepositoryServer.Namespace
serviceName := h.RepositoryServer.Status.ServerInfo.ServiceName
svc := &corev1.Service{}
h.Logger.Info("Check if Service resource exists. If exists, reconcile with CR spec")
h.Logger.Info("Check if the service resource exists. If exists, reconcile with CR spec")
err := h.Reconciler.Get(ctx, types.NamespacedName{Name: serviceName, Namespace: repoServerNamespace}, svc)
if err == nil {
return h.updateService(ctx, svc)
Expand All @@ -93,26 +92,26 @@ func (h *RepoServerHandler) reconcileService(ctx context.Context) (*corev1.Servi
if err != nil {
return nil, err
}
h.Logger.Info("Update serviceName in RepositoryServer /status")
h.Logger.Info("Update service name in RepositoryServer /status")
serverInfo := crv1alpha1.ServerInfo{
PodName: h.RepositoryServer.Status.ServerInfo.PodName,
ServiceName: svc.Name,
}
if err := h.updateServerInfoInCRStatus(ctx, serverInfo); err != nil {
return nil, errors.Wrap(err, "Failed to update ServiceName in RepositoryServer /status")
return nil, errors.Wrap(err, "Failed to update service name in RepositoryServer /status")
}
return svc, err
}

func (h *RepoServerHandler) updateService(ctx context.Context, svc *corev1.Service) (*corev1.Service, error) {
svc = h.updateServiceSpecInService(svc)
svc = h.updateServiceSpec(svc)
if err := h.Reconciler.Update(ctx, svc); err != nil {
return nil, err
}
return svc, nil
}

func (h *RepoServerHandler) updateServiceSpecInService(svc *corev1.Service) *corev1.Service {
func (h *RepoServerHandler) updateServiceSpec(svc *corev1.Service) *corev1.Service {
serviceSpec := corev1.ServiceSpec{
Ports: []corev1.ServicePort{
{
Expand All @@ -129,7 +128,7 @@ func (h *RepoServerHandler) updateServiceSpecInService(svc *corev1.Service) *cor

func (h *RepoServerHandler) createService(ctx context.Context, repoServerNamespace string) (*corev1.Service, error) {
svc := getRepoServerService(repoServerNamespace)
h.Logger.Info("Set controller reference on Service to allow reconciliation using this controller")
h.Logger.Info("Set controller reference on the service to allow reconciliation using this controller")
if err := controllerutil.SetControllerReference(h.RepositoryServer, &svc, h.Reconciler.Scheme); err != nil {
return nil, err
}
Expand Down Expand Up @@ -160,26 +159,26 @@ func (h *RepoServerHandler) reconcilePod(ctx context.Context, svc *corev1.Servic
repoServerNamespace := h.RepositoryServer.Namespace
podName := h.RepositoryServer.Status.ServerInfo.PodName
pod := &corev1.Pod{}
h.Logger.Info("Check if Pod resource exists. If exists, reconcile with CR spec")
h.Logger.Info("Check if the pod resource exists. If exists, reconcile with CR spec")
err := h.Reconciler.Get(ctx, types.NamespacedName{Name: podName, Namespace: repoServerNamespace}, pod)
if err == nil {
return h.updatePod(ctx, pod, svc)
}
if !apierrors.IsNotFound(err) {
return nil, err
}
h.Logger.Info("Pod resource not found. Creating new Pod")
h.Logger.Info("Pod resource not found. Creating new pod")
pod, err = h.createPod(ctx, repoServerNamespace, svc)
if err != nil {
return nil, err
}
h.Logger.Info("Update podName in RepositoryServer /status")
h.Logger.Info("Update pod name in RepositoryServer /status")
serverInfo := crv1alpha1.ServerInfo{
PodName: pod.Name,
ServiceName: h.RepositoryServer.Status.ServerInfo.ServiceName,
}
if err := h.updateServerInfoInCRStatus(ctx, serverInfo); err != nil {
return nil, errors.Wrap(err, "Failed to update podName in RepositoryServer /status")
return nil, errors.Wrap(err, "Failed to update pod name in RepositoryServer /status")
}
return pod, nil
}
Expand All @@ -199,12 +198,12 @@ func (h *RepoServerHandler) updatePod(ctx context.Context, pod *corev1.Pod, svc
}

func (h *RepoServerHandler) updateServiceNameInPodLabels(pod *corev1.Pod, svc *corev1.Service) (*corev1.Pod, error) {
h.Logger.Info("Check if current svcName matches in Pod labels")
h.Logger.Info("Check if current service name matches in pod labels")
if pod.ObjectMeta.Labels[repoServerServiceNameKey] == svc.Name {
h.Logger.Info("Skipping Pod Label update. Current svcName matches with Pod labels")
h.Logger.Info("Skipping pod label update. Current service name matches with the pod labels")
return pod, nil
}
h.Logger.Info("Current svcName does not match in Pod labels. Update Pod with new svcName")
h.Logger.Info("Current service name does not match pod labels. Update pod with new service name")
currentLabel := map[string]string{repoServerServiceNameKey: svc.Name}
pod.ObjectMeta.Labels = currentLabel
return pod, nil
Expand All @@ -220,7 +219,7 @@ func (h *RepoServerHandler) createPod(ctx context.Context, repoServerNamespace s
if err != nil {
return nil, err
}
h.Logger.Info("Set controller reference on Pod to allow reconciliation using this controller")
h.Logger.Info("Set controller reference on the pod to allow reconciliation using this controller")
if err := controllerutil.SetControllerReference(h.RepositoryServer, pod, h.Reconciler.Scheme); err != nil {
return nil, err
}
Expand Down Expand Up @@ -273,7 +272,7 @@ func (h *RepoServerHandler) preparePodOverride(ctx context.Context) (map[string]
}

func (h *RepoServerHandler) updateServerInfoInCRStatus(ctx context.Context, info crv1alpha1.ServerInfo) error {
h.Logger.Info("Fetch latest version of RepositoryServer to update the ServerInfo in it's status")
h.Logger.Info("Fetch latest version of RepositoryServer to update the ServerInfo in its status")
repoServerName := h.RepositoryServer.Name
repoServerNamespace := h.RepositoryServer.Namespace
rs := crv1alpha1.RepositoryServer{}
Expand All @@ -294,7 +293,7 @@ func (h *RepoServerHandler) updateServerInfoInCRStatus(ctx context.Context, info

func (h *RepoServerHandler) waitForPodReady(ctx context.Context, pod *corev1.Pod) error {
if err := kube.WaitForPodReady(ctx, h.KubeCli, pod.Namespace, pod.Name); err != nil {
return errors.Wrap(err, fmt.Sprintf("Failed while waiting for Pod %s to be ready", pod.Name))
return errors.Wrap(err, fmt.Sprintf("Failed while waiting for pod %s to be ready", pod.Name))
}
return nil
}
23 changes: 21 additions & 2 deletions pkg/controllers/repositoryserver/repository.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,17 @@
// Copyright 2023 The Kanister Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package repositoryserver

import (
Expand Down Expand Up @@ -30,6 +44,11 @@ func (h *RepoServerHandler) connectToKopiaRepository() error {
Location: h.RepositoryServerSecrets.storage.Data,
}

return repository.ConnectToKopiaRepository(h.KubeCli, h.RepositoryServer.Namespace, h.RepositoryServer.Status.ServerInfo.PodName, repoServerPodContainerName, args)

return repository.ConnectToKopiaRepository(
h.KubeCli,
h.RepositoryServer.Namespace,
h.RepositoryServer.Status.ServerInfo.PodName,
repoServerPodContainerName,
args,
)
}
30 changes: 14 additions & 16 deletions pkg/controllers/repositoryserver/repositoryserver_controller.go
Original file line number Diff line number Diff line change
@@ -1,18 +1,16 @@
/*
Copyright 2022.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
// Copyright 2023 The Kanister Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package repositoryserver

Expand Down Expand Up @@ -68,7 +66,7 @@ func (r *RepositoryServerReconciler) Reconcile(ctx context.Context, req ctrl.Req
}
kubeCli, err := kubernetes.NewForConfig(cnf)
if err != nil {
return ctrl.Result{}, errors.Wrap(err, "failed to get a k8s client")
return ctrl.Result{}, errors.Wrap(err, "Failed to get a k8s client")
}

logger.Info("Fetch RepositoryServer CR. If not found end reconcile loop")
Expand Down
4 changes: 2 additions & 2 deletions pkg/controllers/repositoryserver/secrets_manager.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2022 The Kanister Authors.
// Copyright 2023 The Kanister Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -37,7 +37,7 @@ func (h *RepoServerHandler) getSecretsFromCR(ctx context.Context) error {
// TODO: For now, users should make sure all the secrets and the RepositoryServer CR are present in the
// same namespace. This namespace field can be overriden when we start creating secrets using 'kanctl' utility
repositoryServer := h.RepositoryServer
h.Logger.Info("Fetching secrets from all the secretReferences in the CR")
h.Logger.Info("Fetching secrets from all the secret references in the CR")
storage, err := h.fetchSecret(ctx, repositoryServer.Spec.Storage.SecretRef)
if err != nil {
return err
Expand Down
Loading

0 comments on commit e4608b2

Please sign in to comment.