func (t *Tester) testCreateAlreadyExisting(obj runtime.Object, createFn CreateFunc) { ctx := t.TestContext() foo := copyOrDie(obj) t.setObjectMeta(foo, t.namer(1)) if err := createFn(ctx, foo); err != nil { t.Errorf("unexpected error: %v", err) } defer t.delete(ctx, foo) _, err := t.storage.(rest.Creater).Create(ctx, foo) if !errors.IsAlreadyExists(err) { t.Errorf("expected already exists err, got %v", err) } }
// same as above func comments, except 'recyclerClient' is a narrower pod API // interface to ease testing func internalRecycleVolumeByWatchingPodUntilCompletion(pvName string, pod *api.Pod, recyclerClient recyclerClient) error { glog.V(5).Infof("creating recycler pod for volume %s\n", pod.Name) // Generate unique name for the recycler pod - we need to get "already // exists" error when a previous controller has already started recycling // the volume. Here we assume that pv.Name is already unique. pod.Name = "recycler-for-" + pvName pod.GenerateName = "" // Start the pod _, err := recyclerClient.CreatePod(pod) if err != nil { if errors.IsAlreadyExists(err) { glog.V(5).Infof("old recycler pod %q found for volume", pod.Name) } else { return fmt.Errorf("Unexpected error creating recycler pod: %+v\n", err) } } defer recyclerClient.DeletePod(pod.Name, pod.Namespace) // Now only the old pod or the new pod run. Watch it until it finishes. stopChannel := make(chan struct{}) defer close(stopChannel) nextPod := recyclerClient.WatchPod(pod.Name, pod.Namespace, stopChannel) for { watchedPod := nextPod() if watchedPod.Status.Phase == api.PodSucceeded { // volume.Recycle() returns nil on success, else error return nil } if watchedPod.Status.Phase == api.PodFailed { // volume.Recycle() returns nil on success, else error if watchedPod.Status.Message != "" { return fmt.Errorf(watchedPod.Status.Message) } else { return fmt.Errorf("pod failed, pod.Status.Message unknown.") } } } }
// recordEvent attempts to write event to a sink. It returns true if the event // was successfully recorded or discarded, false if it should be retried. // If updateExistingEvent is false, it creates a new event, otherwise it updates // existing event. func recordEvent(sink EventSink, event *api.Event, patch []byte, updateExistingEvent bool, eventCorrelator *EventCorrelator) bool { var newEvent *api.Event var err error if updateExistingEvent { newEvent, err = sink.Patch(event, patch) } // Update can fail because the event may have been removed and it no longer exists. if !updateExistingEvent || (updateExistingEvent && isKeyNotFoundError(err)) { // Making sure that ResourceVersion is empty on creation event.ResourceVersion = "" newEvent, err = sink.Create(event) } if err == nil { // we need to update our event correlator with the server returned state to handle name/resourceversion eventCorrelator.UpdateState(newEvent) return true } // If we can't contact the server, then hold everything while we keep trying. // Otherwise, something about the event is malformed and we should abandon it. switch err.(type) { case *restclient.RequestConstructionError: // We will construct the request the same next time, so don't keep trying. glog.Errorf("Unable to construct event '%#v': '%v' (will not retry!)", event, err) return true case *errors.StatusError: if errors.IsAlreadyExists(err) { glog.V(5).Infof("Server rejected event '%#v': '%v' (will not retry!)", event, err) } else { glog.Errorf("Server rejected event '%#v': '%v' (will not retry!)", event, err) } return true case *errors.UnexpectedObjectError: // We don't expect this; it implies the server's response didn't match a // known pattern. Go ahead and retry. default: // This case includes actual http transport errors. Go ahead and retry. } glog.Errorf("Unable to write event: '%v' (may retry after sleeping)", err) return false }
func (mc *basicMirrorClient) CreateMirrorPod(pod *api.Pod) error { if mc.apiserverClient == nil { return nil } // Make a copy of the pod. copyPod := *pod copyPod.Annotations = make(map[string]string) for k, v := range pod.Annotations { copyPod.Annotations[k] = v } hash := getPodHash(pod) copyPod.Annotations[kubetypes.ConfigMirrorAnnotationKey] = hash apiPod, err := mc.apiserverClient.Core().Pods(copyPod.Namespace).Create(©Pod) if err != nil && errors.IsAlreadyExists(err) { // Check if the existing pod is the same as the pod we want to create. if h, ok := apiPod.Annotations[kubetypes.ConfigMirrorAnnotationKey]; ok && h == hash { return nil } } return err }
func TestStoreCreate(t *testing.T) { podA := &api.Pod{ ObjectMeta: api.ObjectMeta{Name: "foo", Namespace: "test"}, Spec: api.PodSpec{NodeName: "machine"}, } podB := &api.Pod{ ObjectMeta: api.ObjectMeta{Name: "foo", Namespace: "test"}, Spec: api.PodSpec{NodeName: "machine2"}, } testContext := api.WithNamespace(api.NewContext(), "test") server, registry := NewTestGenericStoreRegistry(t) defer server.Terminate(t) // create the object objA, err := registry.Create(testContext, podA) if err != nil { t.Errorf("Unexpected error: %v", err) } // get the object checkobj, err := registry.Get(testContext, podA.Name) if err != nil { t.Errorf("Unexpected error: %v", err) } // verify objects are equal if e, a := objA, checkobj; !reflect.DeepEqual(e, a) { t.Errorf("Expected %#v, got %#v", e, a) } // now try to create the second pod _, err = registry.Create(testContext, podB) if !errors.IsAlreadyExists(err) { t.Errorf("Unexpected error: %v", err) } }
// createDefaultServiceAccount creates a default ServiceAccount in the specified namespace func (e *ServiceAccountsController) createServiceAccount(sa api.ServiceAccount, namespace string) { sa.Namespace = namespace if _, err := e.client.Core().ServiceAccounts(namespace).Create(&sa); err != nil && !apierrs.IsAlreadyExists(err) { glog.Error(err) } }