github.com/zppinho/prow@v0.0.0-20240510014325-1738badeb017/cmd/entrypoint/main.go (about)

     1  /*
     2  Copyright 2018 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 main
    18  
    19  import (
    20  	"fmt"
    21  	"os"
    22  	"path"
    23  
    24  	"github.com/sirupsen/logrus"
    25  	"sigs.k8s.io/prow/pkg/entrypoint"
    26  	"sigs.k8s.io/prow/pkg/logrusutil"
    27  	"sigs.k8s.io/prow/pkg/pod-utils/options"
    28  )
    29  
    30  // copy copies entrypoint binary from source to destination. This is because
    31  // entrypoint image operates in two different modes:
    32  //  1. entrypoint container: copy the binary to shared mount drive `/tools`
    33  //  2. test container(s): use `/tools/entrypoint` as entrypoint, for collecting
    34  //     logs and artifacts.
    35  func copy(src, dst string) error {
    36  	logrus.Infof("src is %s", src)
    37  	// Get file info so that the mode can be used for copying
    38  	info, err := os.Stat(src)
    39  	if err != nil {
    40  		return fmt.Errorf("read info '%s': %w", src, err)
    41  	}
    42  	body, err := os.ReadFile(src)
    43  	if err != nil {
    44  		return fmt.Errorf("read file '%s': %w", src, err)
    45  	}
    46  	// Create dir if not exist
    47  	dstDir := path.Dir(dst)
    48  	if err := os.MkdirAll(dstDir, 0755); err != nil {
    49  		return fmt.Errorf("create dir '%s': %w", dstDir, err)
    50  	}
    51  	if err := os.WriteFile(dst, body, info.Mode()); err != nil {
    52  		return fmt.Errorf("write file '%s': %w", dst, err)
    53  	}
    54  	return nil
    55  }
    56  
    57  func main() {
    58  	logrusutil.ComponentInit()
    59  
    60  	o := entrypoint.NewOptions()
    61  	if err := options.Load(o); err != nil {
    62  		logrus.Fatalf("Could not resolve options: %v", err)
    63  	}
    64  
    65  	if o.CopyModeOnly {
    66  		if err := copy(os.Args[0], o.CopyDst); err != nil {
    67  			logrus.WithError(err).Fatal("Failed running in copy mode, this is a prow bug.")
    68  		}
    69  		os.Exit(0)
    70  	}
    71  
    72  	if err := o.Validate(); err != nil {
    73  		logrus.Fatalf("Invalid options: %v", err)
    74  	}
    75  
    76  	os.Exit(o.Run())
    77  }