github.com/kubewharf/katalyst-core@v0.5.3/cmd/katalyst-controller/app/options/options.go (about)

     1  /*
     2  Copyright 2022 The Katalyst 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 options
    18  
    19  import (
    20  	"k8s.io/apimachinery/pkg/util/errors"
    21  	cliflag "k8s.io/component-base/cli/flag"
    22  
    23  	"github.com/kubewharf/katalyst-core/cmd/base/options"
    24  	"github.com/kubewharf/katalyst-core/pkg/config"
    25  )
    26  
    27  // Options holds the configurations for controllers.
    28  type Options struct {
    29  	*options.GenericOptions
    30  
    31  	genericControllerOptions *GenericControllerOptions
    32  	controllersOptions       *ControllersOptions
    33  }
    34  
    35  // NewOptions creates a new Options with a default config.
    36  func NewOptions() *Options {
    37  	return &Options{
    38  		GenericOptions:           options.NewGenericOptions(),
    39  		genericControllerOptions: NewGenericControllerOptions(),
    40  		controllersOptions:       NewControllersOptions(),
    41  	}
    42  }
    43  
    44  // AddFlags adds flags  to the specified FlagSet.
    45  func (o *Options) AddFlags(fss *cliflag.NamedFlagSets) {
    46  	o.GenericOptions.AddFlags(fss)
    47  	o.genericControllerOptions.AddFlags(fss)
    48  	o.controllersOptions.AddFlags(fss)
    49  }
    50  
    51  // ApplyTo fills up config with options
    52  func (o *Options) ApplyTo(c *config.Configuration) error {
    53  	errList := make([]error, 0, 3)
    54  
    55  	errList = append(errList, o.GenericOptions.ApplyTo(c.GenericConfiguration))
    56  	errList = append(errList, o.genericControllerOptions.ApplyTo(c.GenericControllerConfiguration))
    57  	errList = append(errList, o.controllersOptions.ApplyTo(c.ControllersConfiguration))
    58  
    59  	return errors.NewAggregate(errList)
    60  }
    61  
    62  func (o *Options) Config() (*config.Configuration, error) {
    63  	c := config.NewConfiguration()
    64  	if err := o.ApplyTo(c); err != nil {
    65  		return nil, err
    66  	}
    67  	return c, nil
    68  }