github.com/erda-project/erda-infra@v1.0.9/providers/kubernetes/examples/main.go (about)

     1  // Copyright (c) 2021 Terminus, Inc.
     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 main
    16  
    17  import (
    18  	"context"
    19  	"fmt"
    20  	"os"
    21  
    22  	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    23  	"k8s.io/client-go/kubernetes"
    24  
    25  	"github.com/erda-project/erda-infra/base/logs"
    26  
    27  	"github.com/erda-project/erda-infra/base/servicehub"
    28  	pkube "github.com/erda-project/erda-infra/providers/kubernetes"
    29  )
    30  
    31  type provider struct {
    32  	Log    logs.Logger
    33  	Kube   pkube.Interface
    34  	Client *kubernetes.Clientset
    35  }
    36  
    37  func (p *provider) Init(ctx servicehub.Context) error {
    38  	// fmt.Println(p.Kube)
    39  	// fmt.Println(p.Client)
    40  	return nil
    41  }
    42  
    43  func (p *provider) Run(ctx context.Context) error {
    44  	nodes, err := p.Client.CoreV1().Nodes().List(context.Background(), metav1.ListOptions{})
    45  	if err != nil {
    46  		return err
    47  	}
    48  	for _, node := range nodes.Items {
    49  		ip := node.Name
    50  		fmt.Println(ip)
    51  	}
    52  	return nil
    53  }
    54  
    55  func init() {
    56  	servicehub.Register("examples", &servicehub.Spec{
    57  		Services:     []string{"hello"},
    58  		Dependencies: []string{"kubernetes"},
    59  		Description:  "hello for example",
    60  		Creator: func() servicehub.Provider {
    61  			return &provider{}
    62  		},
    63  	})
    64  }
    65  
    66  func main() {
    67  	hub := servicehub.New()
    68  	hub.Run("examples", "", os.Args...)
    69  }