...

Source file src/github.com/cert-manager/issuer-lib/internal/ssaclient/issuer.go

Documentation: github.com/cert-manager/issuer-lib/internal/ssaclient

     1  /*
     2  Copyright 2023 The cert-manager Authors.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package ssaclient
    18  
    19  import (
    20  	"encoding/json"
    21  
    22  	v1 "k8s.io/client-go/applyconfigurations/meta/v1"
    23  	"sigs.k8s.io/controller-runtime/pkg/client"
    24  
    25  	"github.com/cert-manager/issuer-lib/api/v1alpha1"
    26  )
    27  
    28  type issuerApplyConfiguration struct {
    29  	v1.TypeMetaApplyConfiguration    `json:",inline"`
    30  	*v1.ObjectMetaApplyConfiguration `json:"metadata,omitempty"`
    31  	Status                           *v1alpha1.IssuerStatus `json:"status,omitempty"`
    32  }
    33  
    34  func GenerateIssuerStatusPatch(
    35  	issuerType v1alpha1.Issuer,
    36  	name string,
    37  	namespace string,
    38  	status *v1alpha1.IssuerStatus,
    39  ) (client.Object, client.Patch, error) {
    40  	gvk := issuerType.GetObjectKind().GroupVersionKind()
    41  	if (gvk.Group == "") && (gvk.Version == "") && (gvk.Kind == "") {
    42  		panic("first call kubeutil.SetGroupVersionKind on issuerType before passing it to GenerateIssuerStatusPatch")
    43  	}
    44  
    45  	// This object is used to deduce the name & namespace + unmarshall the return value in
    46  	issuerObject := issuerType.DeepCopyObject().(v1alpha1.Issuer)
    47  	issuerObject.SetName(name)
    48  	issuerObject.SetNamespace(namespace)
    49  
    50  	// This object is used to render the patch
    51  	b := &issuerApplyConfiguration{
    52  		ObjectMetaApplyConfiguration: &v1.ObjectMetaApplyConfiguration{},
    53  	}
    54  	b.WithName(name)
    55  	b.WithNamespace(namespace)
    56  	b.WithKind(gvk.Kind)
    57  	b.WithAPIVersion(gvk.GroupVersion().Identifier())
    58  	b.Status = status
    59  
    60  	encodedPatch, err := json.Marshal(b)
    61  	if err != nil {
    62  		return issuerObject, nil, err
    63  	}
    64  
    65  	return issuerObject, applyPatch{encodedPatch}, nil
    66  }
    67  

View as plain text