2023-01-03 12:23:46 +00:00
/ *
Copyright 2023 The Kubernetes 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
import (
"flag"
"fmt"
"os"
"time"
"k8s.io/klog/v2"
2023-08-17 13:53:06 +00:00
nfdgarbagecollector "sigs.k8s.io/node-feature-discovery/pkg/nfd-gc"
2023-01-03 12:23:46 +00:00
"sigs.k8s.io/node-feature-discovery/pkg/version"
)
const (
// ProgramName is the canonical name of this program
2023-08-17 13:53:06 +00:00
ProgramName = "nfd-gc"
2023-01-03 12:23:46 +00:00
)
func main ( ) {
flags := flag . NewFlagSet ( ProgramName , flag . ExitOnError )
printVersion := flags . Bool ( "version" , false , "Print version and exit." )
args := parseArgs ( flags , os . Args [ 1 : ] ... )
if * printVersion {
fmt . Println ( ProgramName , version . Get ( ) )
os . Exit ( 0 )
}
// Assert that the version is known
if version . Undefined ( ) {
2024-12-12 20:14:50 +00:00
klog . InfoS ( "version not set! Set -ldflags \"-X sigs.k8s.io/node-feature-discovery/pkg/version.version=`git describe --tags --dirty --always --match 'v*'`\" during build or run." )
2023-01-03 12:23:46 +00:00
}
2023-08-17 13:53:06 +00:00
// Get new garbage collector instance
gc , err := nfdgarbagecollector . New ( args )
2023-01-03 12:23:46 +00:00
if err != nil {
2023-08-17 13:53:06 +00:00
klog . ErrorS ( err , "failed to initialize nfd garbage collector instance" )
2023-05-03 08:32:53 +00:00
os . Exit ( 1 )
2023-01-03 12:23:46 +00:00
}
if err = gc . Run ( ) ; err != nil {
2023-05-03 08:32:53 +00:00
klog . ErrorS ( err , "error while running" )
os . Exit ( 1 )
2023-01-03 12:23:46 +00:00
}
}
2023-08-17 13:53:06 +00:00
func parseArgs ( flags * flag . FlagSet , osArgs ... string ) * nfdgarbagecollector . Args {
2023-01-03 12:23:46 +00:00
args := initFlags ( flags )
_ = flags . Parse ( osArgs )
if len ( flags . Args ( ) ) > 0 {
fmt . Fprintf ( flags . Output ( ) , "unknown command line argument: %s\n" , flags . Args ( ) [ 0 ] )
flags . Usage ( )
os . Exit ( 2 )
}
return args
}
2023-08-17 13:53:06 +00:00
func initFlags ( flagset * flag . FlagSet ) * nfdgarbagecollector . Args {
args := & nfdgarbagecollector . Args { }
2023-01-03 12:23:46 +00:00
flagset . DurationVar ( & args . GCPeriod , "gc-interval" , time . Duration ( 1 ) * time . Hour ,
2023-08-17 13:53:06 +00:00
"interval between cleanup of obsolete api objects" )
2023-01-03 12:23:46 +00:00
flagset . StringVar ( & args . Kubeconfig , "kubeconfig" , "" ,
"Kubeconfig to use" )
2023-10-09 10:06:07 +00:00
flagset . IntVar ( & args . MetricsPort , "metrics" , 8081 ,
"Port on which to expose metrics." )
2023-01-03 12:23:46 +00:00
klog . InitFlags ( flagset )
return args
}