Пример #1
0
// SetConfig implements SetConfig function of the Service interface.
func (l *KubeListener) SetConfig(config common.ServiceConfig) error {
	confString := "/etc/romana/romana.conf.yml:kubernetesListener:config:"
	log.Trace(trace.Inside, confString, config)

	m := config.ServiceSpecific
	if kl, ok := m["kubernetes_url"]; !ok || kl == "" {
		return fmt.Errorf("%s%s", confString, "kubernetes_url required in config.")
	}
	l.kubeURL = m["kubernetes_url"].(string)

	if nnp, ok := m["namespace_notification_path"]; !ok || nnp == "" {
		return fmt.Errorf("%s%s", confString, "namespace_notification_path required in config.")
	}
	l.namespaceNotificationPath = m["namespace_notification_path"].(string)

	if pnppre, ok := m["policy_notification_path_prefix"]; !ok || pnppre == "" {
		return fmt.Errorf("%s%s", confString, "policy_notification_path_prefix required in config.")
	}
	l.policyNotificationPathPrefix = m["policy_notification_path_prefix"].(string)

	if pnppost, ok := m["policy_notification_path_prefix"]; !ok || pnppost == "" {
		return fmt.Errorf("%s%s", confString, "policy_notification_path_postfix required in config.")
	}
	l.policyNotificationPathPostfix = m["policy_notification_path_postfix"].(string)

	if sln, ok := m["segment_label_name"]; !ok || sln == "" {
		return fmt.Errorf("%s%s", confString, "segment_label_name required in config.")
	}
	l.segmentLabelName = m["segment_label_name"].(string)

	if tln, ok := m["tenant_label_name"]; !ok || tln == "" {
		return fmt.Errorf("%s%s", confString, "tenant_label_name required in config.")
	}
	l.tenantLabelName = m["tenant_label_name"].(string)

	l.namespaceBufferSize = 1000

	if kc, ok := m["kubernetes_config"]; !ok || kc == "" {
		// Default kubernetes config location on ubuntu
		// TODO: this should not be hard coded, other
		//       distributions may have other user names.
		m["kubernetes_config"] = "/home/ubuntu/.kube/config"
	}

	// TODO, this loads kubernetes config from flags provided in main
	// should be loading from path provided by romana-root. Stas.
	kubeClientConfig, err := clientcmd.BuildConfigFromFlags("", m["kubernetes_config"].(string))
	if err != nil {
		return errors.New(fmt.Sprintf("Failed to load kubernetes kubeClientConfig %s", err))
	}
	clientset, err := kubernetes.NewForConfig(kubeClientConfig)
	if err != nil {
		return fmt.Errorf("Failed to make kubernetes client %s", err)
	}
	l.kubeClient = clientset

	return nil
}
Пример #2
0
func createK8sClient(endpoint, kubeCfgPath string) (*k8s.Clientset, error) {
	var (
		config *k8sRest.Config
		err    error
	)
	if kubeCfgPath != "" {
		config, err = k8sClientCmd.BuildConfigFromFlags("", kubeCfgPath)
	} else {
		config = &k8sRest.Config{Host: endpoint}
		err = k8sRest.SetKubernetesDefaults(config)
	}
	if err != nil {
		return nil, err
	}
	return k8s.NewForConfig(config)
}
Пример #3
0
func main() {
	flag.Parse()
	// uses the current context in kubeconfig
	config, err := clientcmd.BuildConfigFromFlags("", *kubeconfig)
	if err != nil {
		panic(err.Error())
	}
	// creates the clientset
	clientset, err := kubernetes.NewForConfig(config)
	if err != nil {
		panic(err.Error())
	}
	for {
		pods, err := clientset.Core().Pods("").List(api.ListOptions{})
		if err != nil {
			panic(err.Error())
		}
		fmt.Printf("There are %d pods in the cluster\n", len(pods.Items))
		time.Sleep(10 * time.Second)
	}
}
Пример #4
0
func main() {
	// Accept a kubernetes config file of try the default location.
	var kubeConfig = flag.String("kubeconfig", os.Getenv("HOME")+"/.kube/config",
		"Kubernetes config file.")
	var romanaConfig = flag.String("romanaconfig", os.Getenv("HOME")+"/.romana.yaml",
		"Romana config file.")
	version := flag.Bool("version", false, "Build Information.")
	flag.Parse()

	if *version {
		fmt.Println(common.BuildInfo())
		return
	}

	if *kubeConfig == "" {
		log.Println("Error: must have kubernetes config files specified.")
		os.Exit(1)
	}

	if err := initConfig(*romanaConfig); err != nil {
		log.Println("Error reading romana config file: ", err)
		os.Exit(1)
	}

	// Since romana config was successful above, now set rootURL from config.
	setRomanaRootURL()

	// Try generating config for kubernetes client-go from flags passed,
	// so that we can connect to kubernetes using them.
	kConfig, err := clientcmd.BuildConfigFromFlags("", *kubeConfig)
	if err != nil {
		log.Println("Error: ", err.Error())
		os.Exit(1)
	}

	// Get a set of REST clients which connect to kubernetes services
	// from the config generated above.
	restClientSet, err := kubernetes.NewForConfig(kConfig)
	if err != nil {
		log.Println("Error: ", err.Error())
		os.Exit(1)
	}

	// Channel for stopping watching node events.
	stop := make(chan struct{}, 1)

	// nodeWatcher is a new ListWatch object created from the specified
	// restClientSet above for watching node events.
	nodeWatcher := cache.NewListWatchFromClient(
		restClientSet.CoreClient,
		"nodes",
		api.NamespaceAll,
		fields.Everything())

	// Setup a notifications for specific events using NewInformer.
	_, nodeInformer := cache.NewInformer(
		nodeWatcher,
		&v1.Node{},
		time.Minute,
		cache.ResourceEventHandlerFuncs{
			AddFunc:    kubernetesAddNodeEventHandler,
			UpdateFunc: kubernetesUpdateNodeEventHandler,
			DeleteFunc: kubernetesDeleteNodeEventHandler,
		},
	)

	log.Println("Starting receving node events.")
	go nodeInformer.Run(stop)

	// Set up channel on which to send signal notifications.
	// We must use a buffered channel or risk missing the signal
	// if we're not ready to receive when the signal is sent.
	c := make(chan os.Signal, 1)
	signal.Notify(c, os.Interrupt)

	// Block until a signal is received.
	<-c

	// Stop watching node events.
	close(stop)
	log.Println("Stopped watching node events and quitting watchnodes.")
}