身份验证问题和glog无法打印日志

时间:2019-01-25 06:33:38

标签: go openshift openshift-enterprise glide-golang

我正在尝试编写一个独立的go程序来验证用户身份。我想查看一下openshift代码中幕后发生的情况,因为我的身份验证有时起作用而有时却失败了。另外,我怎么得到我从openshift得到的错误。 LoginOptions中有Out和ErrOut属性,但我不知道如何使用它们

版本:3.9.0

glide.yaml

package: openshift-application
import:
- package: github.com/golang/glog
- package: github.com/docker/distribution
  repo:    https://github.com/openshift/docker-distribution
  version: release-2.6.0
- package: k8s.io/kube-openapi
  version: 39a7bf85c140f972372c2a0d1ee40adbf0c8bfe1
- package: k8s.io/code-generator
  repo:    https://github.com/openshift/kubernetes-code-generator
  version: release-1.9.1
- package: k8s.io/apimachinery
  repo:    https://github.com/openshift/kubernetes-apimachinery
  version: release-1.9.1
- package: k8s.io/api
  repo:    https://github.com/openshift/kubernetes-api
  version: release-1.9.1
- package: k8s.io/client-go
  repo:    https://github.com/openshift/kubernetes-client-go
  version: release-1.9.1
- package: k8s.io/metrics
  repo:    https://github.com/openshift/kubernetes-metrics
  version: release-1.9.1
- package: k8s.io/apiserver
  repo:    https://github.com/openshift/kubernetes-apiserver
  version: release-1.9.1
- package: k8s.io/kube-aggregator
  repo:    https://github.com/openshift/kube-aggregator
  version: release-1.9.1
- package: k8s.io/apiextensions-apiserver
  repo:    https://github.com/openshift/kubernetes-apiextensions-apiserver
  version: release-1.9.1
- package: k8s.io/kubernetes
  repo:    https://github.com/openshift/kubernetes
  version: release-1.9.1
# openshift second
- package: github.com/openshift/api
  version: release-3.9
- package: github.com/openshift/client-go
  version: release-3.9
# this is pinned because it doesn't have branches
- package: github.com/openshift/imagebuilder
  version: eeb3c28f366cd654c136204f4ab8221593b0e899
- package: github.com/openshift/origin
  version: v3.9.0
  subpackages: 
  - pkg/oc/cli/cmd/login
  - pkg/oc/cli/cmd
  - pkg/oc/admin/project
  - pkg/authorization/generated/internalclientset
  - pkg/client/config
  - pkg/cmd/server/bootstrappolicy
  - pkg/user/generated/internalclientset/typed/user/internalversion
- package: github.com/chai2010/gettext-go/gettext
- package: github.com/davecgh/go-spew/spew

test.go文件

package main
import (
    //"io"
    //"fmt"
    //"io/ioutil"
    "github.com/golang/glog"
    "github.com/spf13/pflag"
    "k8s.io/client-go/tools/clientcmd"
    "github.com/openshift/origin/pkg/client/config"
    "github.com/openshift/origin/pkg/oc/cli/cmd/login"
    //kcmdconfig "k8s.io/kubernetes/pkg/kubectl/cmd/config"
    //kclientcmdapi "k8s.io/client-go/tools/clientcmd/api"
    "github.com/davecgh/go-spew/spew"
)

func main() {
    println("Inside the main function")
    username := "**********"
    password := "**********"
    server := "https://master.na39.openshift.opentlc.com"
    loginOptions := newLoginOptions(server, username, password, true)
    if err := loginOptions.GatherInfo(); err != nil {
        spew.Dump(loginOptions)
        panic(err)
    }

    if loginOptions.Username != "himanshu_gupta01-infosys.com" {
        glog.Fatalf("Unexpected user after authentication: %s", loginOptions)
    } else {
        println("Passed")
        //println(loginOptions.BearerToken)
        spew.Dump(loginOptions)
    }
    glog.Flush()
}
func newLoginOptions(server string, username string, password string, insecure bool) *login.LoginOptions {
    flagset := pflag.NewFlagSet("test-flags", pflag.ContinueOnError)
    flags := []string{}
    clientConfig := defaultClientConfig(flagset)
    flagset.Parse(flags)

    startingConfig, _ := clientConfig.RawConfig()
    loginOptions := &login.LoginOptions{
        Server:             server,
        StartingKubeConfig: &startingConfig,
        Username:           username,
        Password:           password,
        InsecureTLS:        insecure,
    }
    glog.Flush()
    return loginOptions
}


func defaultClientConfig(flags *pflag.FlagSet) clientcmd.ClientConfig {
    loadingRules := &clientcmd.ClientConfigLoadingRules{ExplicitPath: ""}

    flags.StringVar(&loadingRules.ExplicitPath, config.OpenShiftConfigFlagName, "", "Path to the config file to use for CLI requests.")

    overrides := &clientcmd.ConfigOverrides{}
    overrideFlags := clientcmd.RecommendedConfigOverrideFlags("")
    overrideFlags.ContextOverrideFlags.Namespace.ShortName = "n"
    clientcmd.BindOverrideFlags(overrides, flags, overrideFlags)

    clientConfig := clientcmd.NewNonInteractiveDeferredLoadingClientConfig(loadingRules, overrides)

    return clientConfig
}

尝试的命令:

go run test.go -v=10 -vmodule=*=10 -logtostderr
go run test.go -v=10 --vmodule=*=10 --logtostderr

0 个答案:

没有答案