github.com/wmuizelaar/kpt@v0.0.0-20221018115725-bd564717b2ed/pkg/status/poller.go (about)

     1  // Copyright 2021 Google LLC
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //      http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package status
    16  
    17  import (
    18  	"time"
    19  
    20  	"k8s.io/kubectl/pkg/cmd/util"
    21  	"sigs.k8s.io/cli-utils/pkg/kstatus/polling"
    22  	"sigs.k8s.io/cli-utils/pkg/kstatus/polling/clusterreader"
    23  	"sigs.k8s.io/cli-utils/pkg/kstatus/polling/engine"
    24  	"sigs.k8s.io/cli-utils/pkg/kstatus/polling/statusreaders"
    25  	"sigs.k8s.io/cli-utils/pkg/kstatus/watcher"
    26  )
    27  
    28  func NewStatusPoller(f util.Factory) (*polling.StatusPoller, error) {
    29  	mapper, err := f.ToRESTMapper()
    30  	if err != nil {
    31  		return nil, err
    32  	}
    33  
    34  	return polling.NewStatusPollerFromFactory(f, polling.Options{
    35  		CustomStatusReaders: []engine.StatusReader{
    36  			&ConfigConnectorStatusReader{
    37  				Mapper: mapper,
    38  			},
    39  			&RolloutStatusReader{
    40  				Mapper: mapper,
    41  			},
    42  		},
    43  	})
    44  }
    45  
    46  func NewStatusWatcher(f util.Factory) (watcher.StatusWatcher, error) {
    47  	mapper, err := f.ToRESTMapper()
    48  	if err != nil {
    49  		return nil, err
    50  	}
    51  
    52  	dynamicClient, err := f.DynamicClient()
    53  	if err != nil {
    54  		return nil, err
    55  	}
    56  
    57  	return &watcher.DefaultStatusWatcher{
    58  		DynamicClient: dynamicClient,
    59  		Mapper:        mapper,
    60  		ResyncPeriod:  1 * time.Hour,
    61  		StatusReader: statusreaders.NewStatusReader(
    62  			mapper,
    63  			NewConfigConnectorStatusReader(mapper),
    64  			NewRolloutStatusReader(mapper)),
    65  		ClusterReader: &clusterreader.DynamicClusterReader{
    66  			DynamicClient: dynamicClient,
    67  			Mapper:        mapper,
    68  		},
    69  	}, nil
    70  }