...

Source file src/k8s.io/component-base/version/dynamic.go

Documentation: k8s.io/component-base/version

     1  /*
     2  Copyright 2023 The Kubernetes 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 version
    18  
    19  import (
    20  	"fmt"
    21  	"sync/atomic"
    22  
    23  	utilversion "k8s.io/apimachinery/pkg/util/version"
    24  )
    25  
    26  var dynamicGitVersion atomic.Value
    27  
    28  func init() {
    29  	// initialize to static gitVersion
    30  	dynamicGitVersion.Store(gitVersion)
    31  }
    32  
    33  // SetDynamicVersion overrides the version returned as the GitVersion from Get().
    34  // The specified version must be non-empty, a valid semantic version, and must
    35  // match the major/minor/patch version of the default gitVersion.
    36  func SetDynamicVersion(dynamicVersion string) error {
    37  	if err := ValidateDynamicVersion(dynamicVersion); err != nil {
    38  		return err
    39  	}
    40  	dynamicGitVersion.Store(dynamicVersion)
    41  	return nil
    42  }
    43  
    44  // ValidateDynamicVersion ensures the given version is non-empty, a valid semantic version,
    45  // and matched the major/minor/patch version of the default gitVersion.
    46  func ValidateDynamicVersion(dynamicVersion string) error {
    47  	return validateDynamicVersion(dynamicVersion, gitVersion)
    48  }
    49  
    50  func validateDynamicVersion(dynamicVersion, defaultVersion string) error {
    51  	if len(dynamicVersion) == 0 {
    52  		return fmt.Errorf("version must not be empty")
    53  	}
    54  	if dynamicVersion == defaultVersion {
    55  		// allow no-op
    56  		return nil
    57  	}
    58  	vRuntime, err := utilversion.ParseSemantic(dynamicVersion)
    59  	if err != nil {
    60  		return err
    61  	}
    62  	// must match major/minor/patch of default version
    63  	var vDefault *utilversion.Version
    64  	if defaultVersion == "v0.0.0-master+$Format:%H$" {
    65  		// special-case the placeholder value which doesn't parse as a semantic version
    66  		vDefault, err = utilversion.ParseSemantic("v0.0.0-master")
    67  	} else {
    68  		vDefault, err = utilversion.ParseSemantic(defaultVersion)
    69  	}
    70  	if err != nil {
    71  		return err
    72  	}
    73  	if vRuntime.Major() != vDefault.Major() || vRuntime.Minor() != vDefault.Minor() || vRuntime.Patch() != vDefault.Patch() {
    74  		return fmt.Errorf("version %q must match major/minor/patch of default version %q", dynamicVersion, defaultVersion)
    75  	}
    76  	return nil
    77  }
    78  

View as plain text