knative.dev/pkg@v0.0.0-20260602142205-ac97e43f6622/test/logstream/interface.go (about)

     1  /*
     2  Copyright 2019 The Knative 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 logstream
    18  
    19  import (
    20  	"context"
    21  	"fmt"
    22  	"os"
    23  	"strings"
    24  	"sync"
    25  
    26  	"k8s.io/client-go/kubernetes"
    27  	"knative.dev/pkg/system"
    28  	"knative.dev/pkg/test"
    29  	"knative.dev/pkg/test/helpers"
    30  	logstreamv2 "knative.dev/pkg/test/logstream/v2"
    31  )
    32  
    33  type (
    34  	// Canceler is the type of function returned when a logstream is started to be
    35  	// deferred so that the logstream can be stopped when the test is complete.
    36  	Canceler = logstreamv2.Canceler
    37  )
    38  
    39  type ti interface {
    40  	Name() string
    41  	Error(args ...interface{})
    42  	Log(args ...interface{})
    43  	Logf(fmt string, args ...interface{})
    44  }
    45  
    46  // Start begins streaming the logs from system components with a `key:` matching
    47  // `test.ObjectNameForTest(t)` to `t.Log`. It returns a Canceler, which must
    48  // be called before the test completes.
    49  func Start(t ti) Canceler {
    50  	// Do this lazily to make import ordering less important.
    51  	once.Do(func() {
    52  		if ns := os.Getenv(system.NamespaceEnvKey); ns != "" {
    53  			var err error
    54  			// handle case when ns contains a csv list
    55  			namespaces := strings.Split(ns, ",")
    56  			if sysStream, err = initStream(namespaces); err != nil {
    57  				t.Error("Error initializing logstream", "error", err)
    58  			}
    59  		} else {
    60  			// Otherwise, set up a null stream.
    61  			sysStream = &null{}
    62  		}
    63  	})
    64  
    65  	return sysStream.Start(t)
    66  }
    67  
    68  func initStream(namespaces []string) (streamer, error) {
    69  	config, err := test.Flags.GetRESTConfig()
    70  	if err != nil {
    71  		return &null{}, fmt.Errorf("error loading client config: %w", err)
    72  	}
    73  
    74  	kc, err := kubernetes.NewForConfig(config)
    75  	if err != nil {
    76  		return &null{}, fmt.Errorf("error creating kubernetes client: %w", err)
    77  	}
    78  
    79  	return &shim{logstreamv2.FromNamespaces(context.Background(), kc, namespaces)}, nil
    80  }
    81  
    82  type streamer interface {
    83  	Start(t ti) Canceler
    84  }
    85  
    86  var (
    87  	sysStream streamer
    88  	once      sync.Once
    89  )
    90  
    91  type shim struct {
    92  	logstreamv2.Source
    93  }
    94  
    95  func (s *shim) Start(t ti) Canceler {
    96  	name := helpers.ObjectPrefixForTest(t)
    97  	canceler, err := s.StartStream(name, t.Logf)
    98  	if err != nil {
    99  		t.Error("Failed to start logstream", "error", err)
   100  	}
   101  
   102  	return canceler
   103  }