github.com/IBM-Blockchain/fabric-operator@v1.0.4/pkg/action/action.go (about)

     1  /*
     2   * Copyright contributors to the Hyperledger Fabric Operator project
     3   *
     4   * SPDX-License-Identifier: Apache-2.0
     5   *
     6   * Licensed under the Apache License, Version 2.0 (the "License");
     7   * you may not use this file except in compliance with the License.
     8   * You may obtain a copy of the License at:
     9   *
    10   * 	  http://www.apache.org/licenses/LICENSE-2.0
    11   *
    12   * Unless required by applicable law or agreed to in writing, software
    13   * distributed under the License is distributed on an "AS IS" BASIS,
    14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    15   * See the License for the specific language governing permissions and
    16   * limitations under the License.
    17   */
    18  
    19  package action
    20  
    21  import (
    22  	"context"
    23  	"fmt"
    24  	"time"
    25  
    26  	"github.com/IBM-Blockchain/fabric-operator/pkg/initializer/common"
    27  	k8sclient "github.com/IBM-Blockchain/fabric-operator/pkg/k8s/controllerclient"
    28  
    29  	appsv1 "k8s.io/api/apps/v1"
    30  	v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    31  	"k8s.io/apimachinery/pkg/runtime"
    32  	"k8s.io/apimachinery/pkg/types"
    33  	runtimeclient "sigs.k8s.io/controller-runtime/pkg/client"
    34  	logf "sigs.k8s.io/controller-runtime/pkg/log"
    35  )
    36  
    37  var log = logf.Log.WithName("action")
    38  
    39  // By triggering a component restart by updating its annotations instead of deleting
    40  // the deployment, components will be restarted with the rolling update strategy
    41  // unless their deployments specify a recreate strategy. This will allow ibpconsole
    42  // components with rolling update strategies to not have any downtime.
    43  func Restart(client k8sclient.Client, name, namespace string) error {
    44  	deployment := &appsv1.Deployment{}
    45  	err := client.Get(context.TODO(), types.NamespacedName{Name: name, Namespace: namespace}, deployment)
    46  	if err != nil {
    47  		return err
    48  	}
    49  
    50  	if deployment == nil {
    51  		return fmt.Errorf("failed to get deployment %s", name)
    52  	}
    53  
    54  	if deployment.Spec.Template.ObjectMeta.Annotations == nil {
    55  		deployment.Spec.Template.ObjectMeta.Annotations = make(map[string]string)
    56  	}
    57  	deployment.Spec.Template.ObjectMeta.Annotations["kubectl.kubernetes.io/restartedAt"] = time.Now().Format(time.RFC3339)
    58  
    59  	err = client.Patch(context.TODO(), deployment, nil, k8sclient.PatchOption{
    60  		Resilient: &k8sclient.ResilientPatch{
    61  			Retry:    3,
    62  			Into:     &appsv1.Deployment{},
    63  			Strategy: runtimeclient.MergeFrom,
    64  		},
    65  	})
    66  	if err != nil {
    67  		return err
    68  	}
    69  
    70  	return nil
    71  }
    72  
    73  //go:generate counterfeiter -o mocks/reenroller.go -fake-name Reenroller . Reenroller
    74  
    75  type Reenroller interface {
    76  	RenewCert(certType common.SecretType, instance runtime.Object, newKey bool) error
    77  }
    78  
    79  //go:generate counterfeiter -o mocks/reenrollinstance.go -fake-name ReenrollInstance . ReenrollInstance
    80  
    81  type ReenrollInstance interface {
    82  	runtime.Object
    83  	v1.Object
    84  	ResetEcertReenroll()
    85  	ResetTLSReenroll()
    86  }
    87  
    88  func Reenroll(reenroller Reenroller, client k8sclient.Client, certType common.SecretType, instance ReenrollInstance, newKey bool) error {
    89  	err := reenroller.RenewCert(certType, instance, newKey)
    90  	if err != nil {
    91  		return err
    92  	}
    93  
    94  	return nil
    95  }