本文整理汇总了Golang中k8s/io/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: newCondition
func newCondition() experimental.JobCondition {
return experimental.JobCondition{
Type: experimental.JobComplete,
Status: api.ConditionTrue,
LastProbeTime: util.Now(),
LastTransitionTime: 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)
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
}
示例4: 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
}
示例5: 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
}
示例6: TestAddOrUpdateEventNoExisting
func TestAddOrUpdateEventNoExisting(t *testing.T) {
// Arrange
eventTime := util.Now()
event := api.Event{
Reason: "my reasons are many",
Message: "my message is love",
InvolvedObject: api.ObjectReference{
Kind: "Pod",
Name: "awesome.name",
Namespace: "betterNamespace",
UID: "C934D34AFB20242",
APIVersion: "version",
},
Source: api.EventSource{
Component: "kubelet",
Host: "kublet.node1",
},
Count: 1,
FirstTimestamp: eventTime,
LastTimestamp: eventTime,
}
// Act
result := addOrUpdateEvent(&event)
// Assert
compareEventWithHistoryEntry(&event, &result, t)
}
示例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: 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(item *kapi.ReplicationController) bool {
return item.CreationTimestamp.Before(before)
}
}
示例9: 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))
controllers := []*kapi.ReplicationController{
{
ObjectMeta: kapi.ObjectMeta{
Name: "past",
CreationTimestamp: past,
},
},
{
ObjectMeta: kapi.ObjectMeta{
Name: "present",
CreationTimestamp: present,
},
},
}
sort.Sort(sortableReplicationControllers(controllers))
if controllers[0].Name != "present" {
t.Errorf("Unexpected sort order")
}
if controllers[1].Name != "past" {
t.Errorf("Unexpected sort order")
}
}
示例10: 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)
}
}
示例11: TestFilterBeforePredicate
func TestFilterBeforePredicate(t *testing.T) {
youngerThan := time.Hour
now := util.Now()
old := util.NewTime(now.Time.Add(-1 * youngerThan))
builds := []*buildapi.Build{
{
ObjectMeta: kapi.ObjectMeta{
Name: "old",
CreationTimestamp: old,
},
},
{
ObjectMeta: kapi.ObjectMeta{
Name: "new",
CreationTimestamp: now,
},
},
}
filter := &andFilter{
filterPredicates: []FilterPredicate{NewFilterBeforePredicate(youngerThan)},
}
result := filter.Filter(builds)
if len(result) != 1 {
t.Errorf("Unexpected number of results")
}
if expected, actual := "old", result[0].Name; expected != actual {
t.Errorf("expected %v, actual %v", expected, actual)
}
}
示例12: TestDeleteNamespaceWithCompleteFinalizers
func TestDeleteNamespaceWithCompleteFinalizers(t *testing.T) {
now := util.Now()
fakeEtcdClient, etcdStorage := newEtcdStorage(t)
fakeEtcdClient.ChangeIndex = 1
key := etcdtest.AddPrefix("/namespaces/foo")
fakeEtcdClient.Data[key] = 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{},
},
Status: api.NamespaceStatus{Phase: api.NamespaceActive},
}),
ModifiedIndex: 1,
CreatedIndex: 1,
},
},
}
storage, _, _ := NewStorage(etcdStorage)
_, err := storage.Delete(api.NewContext(), "foo", nil)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
}
示例13: Create
func (m *VirtualStorage) Create(ctx kapi.Context, obj runtime.Object) (runtime.Object, error) {
if err := rest.BeforeCreate(m.CreateStrategy, ctx, obj); err != nil {
return nil, err
}
role := obj.(*authorizationapi.Role)
policy, err := m.EnsurePolicy(ctx)
if err != nil {
return nil, err
}
if _, exists := policy.Roles[role.Name]; exists {
return nil, kapierrors.NewAlreadyExists("Role", role.Name)
}
role.ResourceVersion = policy.ResourceVersion
policy.Roles[role.Name] = role
policy.LastModified = util.Now()
if err := m.PolicyStorage.UpdatePolicy(ctx, policy); err != nil {
return nil, err
}
return role, nil
}
示例14: CancelBuild
// CancelBuild updates a build status to Cancelled, after its associated pod is deleted.
func (bc *BuildController) CancelBuild(build *buildapi.Build) error {
if !isBuildCancellable(build) {
glog.V(4).Infof("Build %s/%s can be cancelled only if it has pending/running status, not %s.", build.Namespace, build.Name, build.Status.Phase)
return nil
}
glog.V(4).Infof("Cancelling Build %s/%s.", build.Namespace, build.Name)
pod, err := bc.PodManager.GetPod(build.Namespace, buildutil.GetBuildPodName(build))
if err != nil {
if !errors.IsNotFound(err) {
return fmt.Errorf("Failed to get Pod for build %s/%s: %v", build.Namespace, build.Name, err)
}
} else {
err := bc.PodManager.DeletePod(build.Namespace, pod)
if err != nil && !errors.IsNotFound(err) {
return fmt.Errorf("Couldn't delete Build Pod %s/%s: %v", build.Namespace, pod.Name, err)
}
}
build.Status.Phase = buildapi.BuildPhaseCancelled
now := util.Now()
build.Status.CompletionTimestamp = &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 was successfully cancelled.", build.Namespace, build.Name)
return nil
}
示例15: TestLimitedLogAndRetryFinish
func TestLimitedLogAndRetryFinish(t *testing.T) {
updater := &buildUpdater{}
err := errors.New("funky error")
now := kutil.Now()
retry := controller.Retry{
Count: 0,
StartTimestamp: kutil.Date(now.Year(), now.Month(), now.Day(), now.Hour(), now.Minute()-31, now.Second(), now.Nanosecond(), now.Location()),
}
if limitedLogAndRetry(updater, 30*time.Minute)(&buildapi.Build{Status: buildapi.BuildStatus{Phase: buildapi.BuildPhaseNew}}, err, retry) {
t.Error("Expected no more retries after reaching timeout!")
}
if updater.Build == nil {
t.Fatal("BuildUpdater wasn't called!")
}
if updater.Build.Status.Phase != buildapi.BuildPhaseFailed {
t.Errorf("Expected status %s, got %s!", buildapi.BuildPhaseFailed, updater.Build.Status.Phase)
}
if !strings.Contains(updater.Build.Status.Message, err.Error()) {
t.Errorf("Expected message to contain %v, got %s!", err.Error(), updater.Build.Status.Message)
}
if updater.Build.Status.CompletionTimestamp == nil {
t.Error("Expected CompletionTimestamp to be set!")
}
}