本文整理匯總了Golang中github.com/GoogleCloudPlatform/kubernetes/pkg/util.Now函數的典型用法代碼示例。如果您正苦於以下問題:Golang Now函數的具體用法?Golang Now怎麽用?Golang Now使用的例子?那麽, 這裏精選的函數代碼示例或許可以為您提供幫助。
在下文中一共展示了Now函數的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1: tagReferenceToTagEvent
func tagReferenceToTagEvent(stream *api.ImageStream, tagRef api.TagReference, tagOrID string) (*api.TagEvent, error) {
switch tagRef.From.Kind {
case "DockerImage":
return &api.TagEvent{
Created: util.Now(),
DockerImageReference: tagRef.From.Name,
}, nil
case "ImageStreamImage":
ref, err := api.DockerImageReferenceForStream(stream)
if err != nil {
return nil, err
}
resolvedIDs := api.ResolveImageID(stream, tagOrID)
switch len(resolvedIDs) {
case 1:
ref.ID = resolvedIDs.List()[0]
return &api.TagEvent{
Created: util.Now(),
DockerImageReference: ref.String(),
Image: ref.ID,
}, nil
case 0:
return nil, fmt.Errorf("no images match the prefix %q", tagOrID)
default:
return nil, fmt.Errorf("multiple images match the prefix %q: %s", tagOrID, strings.Join(resolvedIDs.List(), ", "))
}
case "ImageStreamTag":
return api.LatestTaggedImage(stream, tagOrID), nil
default:
return nil, fmt.Errorf("invalid from.kind %q: it must be ImageStreamImage or ImageStreamTag", tagRef.From.Kind)
}
}
示例2: checkNodeReady
// checkNodeReady checks raw node ready condition, without transition timestamp set.
func (s *NodeController) checkNodeReady(node *api.Node) *api.NodeCondition {
switch status, err := s.kubeletClient.HealthCheck(node.Name); {
case err != nil:
glog.V(2).Infof("NodeController: node %s health check error: %v", node.Name, err)
return &api.NodeCondition{
Type: api.NodeReady,
Status: api.ConditionUnknown,
Reason: fmt.Sprintf("Node health check error: %v", err),
LastProbeTime: util.Now(),
}
case status == probe.Failure:
return &api.NodeCondition{
Type: api.NodeReady,
Status: api.ConditionNone,
Reason: fmt.Sprintf("Node health check failed: kubelet /healthz endpoint returns not ok"),
LastProbeTime: util.Now(),
}
default:
return &api.NodeCondition{
Type: api.NodeReady,
Status: api.ConditionFull,
Reason: fmt.Sprintf("Node health check succeeded: kubelet /healthz endpoint returns ok"),
LastProbeTime: util.Now(),
}
}
}
示例3: HandlePod
// HandlePod updates the state of the build based on the pod state
func (bc *BuildPodController) HandlePod(pod *kapi.Pod) error {
obj, exists, err := bc.BuildStore.Get(buildKey(pod))
if err != nil {
glog.V(4).Infof("Error getting Build for pod %s/%s: %v", pod.Namespace, pod.Name, err)
return err
}
if !exists || obj == nil {
glog.V(5).Infof("No Build found for pod %s/%s", pod.Namespace, pod.Name)
return nil
}
build := obj.(*buildapi.Build)
// A cancelling event was triggered for the build, delete its pod and update build status.
if build.Status.Cancelled {
glog.V(4).Infof("Cancelling Build %s/%s.", build.Namespace, build.Name)
if err := bc.CancelBuild(build, pod); err != nil {
return fmt.Errorf("failed to cancel Build %s/%s: %v, will retry", build.Namespace, build.Name, err)
}
return nil
}
nextStatus := build.Status.Phase
switch pod.Status.Phase {
case kapi.PodRunning:
// The pod's still running
nextStatus = buildapi.BuildPhaseRunning
case kapi.PodSucceeded, kapi.PodFailed:
// Check the exit codes of all the containers in the pod
nextStatus = buildapi.BuildPhaseComplete
for _, info := range pod.Status.ContainerStatuses {
if info.State.Terminated != nil && info.State.Terminated.ExitCode != 0 {
nextStatus = buildapi.BuildPhaseFailed
break
}
}
}
if build.Status.Phase != nextStatus {
glog.V(4).Infof("Updating Build %s/%s status %s -> %s", build.Namespace, build.Name, build.Status.Phase, nextStatus)
build.Status.Phase = nextStatus
if buildutil.IsBuildComplete(build) {
now := util.Now()
build.Status.CompletionTimestamp = &now
}
if build.Status.Phase == buildapi.BuildPhaseRunning {
now := util.Now()
build.Status.StartTimestamp = &now
}
if err := bc.BuildUpdater.Update(build.Namespace, build); err != nil {
return fmt.Errorf("failed to update Build %s/%s: %v", build.Namespace, build.Name, err)
}
glog.V(4).Infof("Build %s/%s status was updated %s -> %s", build.Namespace, build.Name, build.Status.Phase, nextStatus)
}
return nil
}
示例4: HandlePod
// HandlePod updates the state of the build based on the pod state
func (bc *BuildPodController) HandlePod(pod *kapi.Pod) error {
obj, exists, err := bc.BuildStore.Get(buildKey(pod))
if err != nil {
glog.V(4).Infof("Error getting Build for pod %s/%s: %v", pod.Namespace, pod.Name, err)
return err
}
if !exists || obj == nil {
glog.V(5).Infof("No Build found for pod %s/%s", pod.Namespace, pod.Name)
return nil
}
build := obj.(*buildapi.Build)
nextStatus := build.Status.Phase
switch pod.Status.Phase {
case kapi.PodRunning:
// The pod's still running
nextStatus = buildapi.BuildPhaseRunning
case kapi.PodSucceeded:
// Check the exit codes of all the containers in the pod
nextStatus = buildapi.BuildPhaseComplete
if len(pod.Status.ContainerStatuses) == 0 {
// no containers in the pod means something went badly wrong, so the build
// should be failed.
glog.V(2).Infof("Failing build %s/%s because the pod has no containers", build.Namespace, build.Name)
nextStatus = buildapi.BuildPhaseFailed
} else {
for _, info := range pod.Status.ContainerStatuses {
if info.State.Terminated != nil && info.State.Terminated.ExitCode != 0 {
nextStatus = buildapi.BuildPhaseFailed
break
}
}
}
case kapi.PodFailed:
nextStatus = buildapi.BuildPhaseFailed
}
if build.Status.Phase != nextStatus {
glog.V(4).Infof("Updating Build %s/%s status %s -> %s", build.Namespace, build.Name, build.Status.Phase, nextStatus)
build.Status.Phase = nextStatus
if buildutil.IsBuildComplete(build) {
now := util.Now()
build.Status.CompletionTimestamp = &now
}
if build.Status.Phase == buildapi.BuildPhaseRunning {
now := util.Now()
build.Status.StartTimestamp = &now
}
if err := bc.BuildUpdater.Update(build.Namespace, build); err != nil {
return fmt.Errorf("failed to update Build %s/%s: %v", build.Namespace, build.Name, err)
}
glog.V(4).Infof("Build %s/%s status was updated %s -> %s", build.Namespace, build.Name, build.Status.Phase, nextStatus)
}
return nil
}
示例5: NewEmptyPolicy
func NewEmptyPolicy(namespace string) *authorizationapi.Policy {
policy := &authorizationapi.Policy{}
policy.Name = authorizationapi.PolicyName
policy.Namespace = namespace
policy.CreationTimestamp = util.Now()
policy.LastModified = util.Now()
policy.Roles = make(map[string]*authorizationapi.Role)
return policy
}
示例6: NewEmptyPolicyBinding
func NewEmptyPolicyBinding(namespace, policyNamespace, policyBindingName string) *authorizationapi.PolicyBinding {
binding := &authorizationapi.PolicyBinding{}
binding.Name = policyBindingName
binding.Namespace = namespace
binding.CreationTimestamp = util.Now()
binding.LastModified = util.Now()
binding.PolicyRef = kapi.ObjectReference{Name: authorizationapi.PolicyName, Namespace: policyNamespace}
binding.RoleBindings = make(map[string]*authorizationapi.RoleBinding)
return binding
}
示例7: TestGetEventExisting
func TestGetEventExisting(t *testing.T) {
// Arrange
eventTime := util.Now()
event := api.Event{
Reason: "do I exist",
Message: "I do, oh my",
InvolvedObject: api.ObjectReference{
Kind: "Pod",
Name: "clever.name.here",
Namespace: "spaceOfName",
UID: "D933D32AFB2A238",
APIVersion: "version",
},
Source: api.EventSource{
Component: "kubelet",
Host: "kublet.node4",
},
Count: 1,
FirstTimestamp: eventTime,
LastTimestamp: eventTime,
}
addOrUpdateEvent(&event)
// Act
existingEvent := getEvent(&event)
// Assert
compareEventWithHistoryEntry(&event, &existingEvent, t)
}
示例8: TestBuildDecorator
func TestBuildDecorator(t *testing.T) {
build := &buildapi.Build{
ObjectMeta: kapi.ObjectMeta{Name: "buildid", Namespace: "default"},
Parameters: buildapi.BuildParameters{
Source: buildapi.BuildSource{
Type: buildapi.BuildSourceGit,
Git: &buildapi.GitBuildSource{
URI: "http://github.com/my/repository",
},
ContextDir: "context",
},
Strategy: buildapi.BuildStrategy{
Type: buildapi.DockerBuildStrategyType,
DockerStrategy: &buildapi.DockerBuildStrategy{},
},
Output: buildapi.BuildOutput{
DockerImageReference: "repository/data",
},
},
Status: buildapi.BuildStatusNew,
}
now := util.Now()
startTime := util.NewTime(now.Time.Add(-1 * time.Minute))
build.StartTimestamp = &startTime
err := Decorator(build)
if err != nil {
t.Errorf("Unexpected error decorating build")
}
if build.Duration <= 0 {
t.Errorf("Build duration should be greater than zero")
}
}
示例9: TestNamespaceStatusStrategy
func TestNamespaceStatusStrategy(t *testing.T) {
ctx := api.NewDefaultContext()
if StatusStrategy.NamespaceScoped() {
t.Errorf("Namespaces should not be namespace scoped")
}
if StatusStrategy.AllowCreateOnUpdate() {
t.Errorf("Namespaces should not allow create on update")
}
now := util.Now()
oldNamespace := &api.Namespace{
ObjectMeta: api.ObjectMeta{Name: "foo", ResourceVersion: "10"},
Spec: api.NamespaceSpec{Finalizers: []api.FinalizerName{"kubernetes"}},
Status: api.NamespaceStatus{Phase: api.NamespaceActive},
}
namespace := &api.Namespace{
ObjectMeta: api.ObjectMeta{Name: "foo", ResourceVersion: "9", DeletionTimestamp: &now},
Status: api.NamespaceStatus{Phase: api.NamespaceTerminating},
}
StatusStrategy.PrepareForUpdate(namespace, oldNamespace)
if namespace.Status.Phase != api.NamespaceTerminating {
t.Errorf("Namespace status updates should allow change of phase: %v", namespace.Status.Phase)
}
if len(namespace.Spec.Finalizers) != 1 || namespace.Spec.Finalizers[0] != api.FinalizerKubernetes {
t.Errorf("PrepareForUpdate should have preserved old finalizers")
}
errs := StatusStrategy.ValidateUpdate(ctx, namespace, oldNamespace)
if len(errs) != 0 {
t.Errorf("Unexpected error %v", errs)
}
if namespace.ResourceVersion != "9" {
t.Errorf("Incoming resource version on update should not be mutated")
}
}
示例10: TestEventCreate
func TestEventCreate(t *testing.T) {
objReference := &api.ObjectReference{
Kind: "foo",
Namespace: "nm",
Name: "objref1",
UID: "uid",
APIVersion: "apiv1",
ResourceVersion: "1",
}
timeStamp := util.Now()
event := &api.Event{
//namespace: namespace{"default"},
Condition: "Running",
InvolvedObject: *objReference,
Timestamp: timeStamp,
}
c := &testClient{
Request: testRequest{
Method: "POST",
Path: "/events",
Body: event,
},
Response: Response{StatusCode: 200, Body: event},
}
response, err := c.Setup().Events("").Create(event)
if err != nil {
t.Errorf("%#v should be nil.", err)
}
if e, a := *objReference, response.InvolvedObject; !reflect.DeepEqual(e, a) {
t.Errorf("%#v != %#v.", e, a)
}
}
示例11: TestDeleteNamespaceWithIncompleteFinalizers
func TestDeleteNamespaceWithIncompleteFinalizers(t *testing.T) {
now := util.Now()
fakeEtcdClient, helper := newHelper(t)
fakeEtcdClient.ChangeIndex = 1
fakeEtcdClient.Data["/registry/namespaces/foo"] = tools.EtcdResponseWithError{
R: &etcd.Response{
Node: &etcd.Node{
Value: runtime.EncodeOrDie(latest.Codec, &api.Namespace{
ObjectMeta: api.ObjectMeta{
Name: "foo",
DeletionTimestamp: &now,
},
Spec: api.NamespaceSpec{
Finalizers: []api.FinalizerName{api.FinalizerKubernetes},
},
Status: api.NamespaceStatus{Phase: api.NamespaceActive},
}),
ModifiedIndex: 1,
CreatedIndex: 1,
},
},
}
storage, _, _ := NewStorage(helper)
_, err := storage.Delete(api.NewDefaultContext(), "foo", nil)
if err == nil {
t.Fatalf("expected error: %v", err)
}
}
示例12: NewFilterBeforePredicate
// NewFilterBeforePredicate is a function that returns true if the build was created before the current time minus specified duration
func NewFilterBeforePredicate(d time.Duration) FilterPredicate {
now := util.Now()
before := util.NewTime(now.Time.Add(-1 * d))
return func(build *buildapi.Build) bool {
return build.CreationTimestamp.Before(before)
}
}
示例13: Create
func (rs *REST) Create(ctx api.Context, obj runtime.Object) (<-chan apiserver.RESTResult, error) {
minion, ok := obj.(*api.Minion)
if !ok {
return nil, fmt.Errorf("not a minion: %#v", obj)
}
if minion.Name == "" {
return nil, fmt.Errorf("ID should not be empty: %#v", minion)
}
minion.CreationTimestamp = util.Now()
return apiserver.MakeAsync(func() (runtime.Object, error) {
err := rs.registry.CreateMinion(ctx, minion)
if err != nil {
return nil, err
}
minionName := minion.Name
minion, err := rs.registry.GetMinion(ctx, minionName)
if err == ErrNotHealty {
return rs.toApiMinion(minionName), nil
}
if minion == nil {
return nil, ErrDoesNotExist
}
if err != nil {
return nil, err
}
return minion, nil
}), nil
}
示例14: TestSort
// TestSort verifies that builds are sorted by most recently created
func TestSort(t *testing.T) {
present := util.Now()
past := util.NewTime(present.Time.Add(-1 * time.Minute))
builds := []*buildapi.Build{
{
ObjectMeta: kapi.ObjectMeta{
Name: "past",
CreationTimestamp: past,
},
},
{
ObjectMeta: kapi.ObjectMeta{
Name: "present",
CreationTimestamp: present,
},
},
}
sort.Sort(sortableBuilds(builds))
if builds[0].Name != "present" {
t.Errorf("Unexpected sort order")
}
if builds[1].Name != "past" {
t.Errorf("Unexpected sort order")
}
}
示例15: Create
func (rs *REST) Create(obj runtime.Object) (<-chan runtime.Object, error) {
minion, ok := obj.(*api.Minion)
if !ok {
return nil, fmt.Errorf("not a minion: %#v", obj)
}
if minion.ID == "" {
return nil, fmt.Errorf("ID should not be empty: %#v", minion)
}
minion.CreationTimestamp = util.Now()
return apiserver.MakeAsync(func() (runtime.Object, error) {
err := rs.registry.Insert(minion.ID)
if err != nil {
return nil, err
}
contains, err := rs.registry.Contains(minion.ID)
if err != nil {
return nil, err
}
if contains {
return rs.toApiMinion(minion.ID), nil
}
return nil, fmt.Errorf("unable to add minion %#v", minion)
}), nil
}